forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
68 lines (51 loc) · 1.43 KB
/
main2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/// Source : https://leetcode.com/problems/merge-k-sorted-lists/description/
/// Author : liuyubobobo
/// Time : 2018-07-02
#include <iostream>
#include <vector>
#include <cassert>
#include <queue>
using namespace std;
/// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class CompareListNode{
public:
bool operator()(ListNode* node1, ListNode* node2){
return node1->val > node2->val;
}
};
/// Using Priority Queue to compare each ListNode
/// Time Complexity: O(nlogk) where k is len(lists) and n is the nodes number
/// Space Complexity: O(1)
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
if(lists.size() == 0)
return NULL;
ListNode* dummyHead = new ListNode(-1);
ListNode* curNode = dummyHead;
priority_queue<ListNode*, vector<ListNode*>, CompareListNode> q;
for(ListNode* node: lists)
if(node != NULL)
q.push(node);
while(!q.empty()){
ListNode* nextNode = q.top();
q.pop();
curNode->next = nextNode;
if(nextNode->next != NULL)
q.push(nextNode->next);
nextNode->next = NULL;
curNode = curNode->next;
}
ListNode* ret = dummyHead->next;
delete dummyHead;
return ret;
}
};
int main() {
return 0;
}