-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergeKLists.cpp
62 lines (58 loc) · 1.49 KB
/
mergeKLists.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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
struct Index {
int val;
int index;
Index( int a= 0, int b= 0 ):
val(a), index(b) {}
};
struct cmp
{
bool operator()(Index a,Index b){
return a.val > b.val;
}
};
ListNode *mergeKLists(vector<ListNode *> &lists) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ListNode *head = NULL;
ListNode *tmp_head = NULL;
if(lists.size() == 0)
return NULL;
priority_queue<Index,vector<Index>,cmp> pq;
for(int i = 0; i < lists.size(); i++) {
if(lists[i]) {
Index a(lists[i]->val,i);
pq.push(a);
}
}
while(!pq.empty()) {
Index tmp = pq.top();
pq.pop();
ListNode *p = lists[tmp.index];
lists[tmp.index] = p->next;
if(lists[tmp.index]) {
tmp.val = lists[tmp.index]->val;
pq.push(tmp);
}
if(!head) {
head = p;
tmp_head = p;
}
else {
tmp_head->next = p;
tmp_head = tmp_head->next;
tmp_head->next = NULL;
}
}
return head;
}
};