forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0023.cpp
More file actions
36 lines (31 loc) · 833 Bytes
/
0023.cpp
File metadata and controls
36 lines (31 loc) · 833 Bytes
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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
ListNode* dummy = new ListNode(0);
ListNode* curr = dummy;
priority_queue<ListNode*, vector<ListNode*>, compareListNode> pq;
for (auto list : lists)
if (list) pq.push(list);
while (!pq.empty()) {
curr->next = pq.top();
pq.pop();
curr = curr->next;
if (curr->next) pq.push(curr->next);
}
return dummy->next;
}
private:
struct compareListNode {
bool operator()(const ListNode* l1, const ListNode* l2) {
return l1->val > l2->val;
}
};
};