-
Notifications
You must be signed in to change notification settings - Fork 2
/
023 Merge k Sorted Lists.cpp
63 lines (51 loc) · 1.33 KB
/
023 Merge k Sorted Lists.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
/*
23 Merge k Sorted Lists
https://oj.leetcode.com/problems/merge-k-sorted-lists/
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
Example:
Input:
[
1->4->5,
1->3->4,
2->6
]
Output: 1->1->2->3->4->4->5->6
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
#include <queue>
class Solution {
public:
ListNode* mergeKLists(std::vector<ListNode*>& lists)
{
auto* head = new ListNode(0); // NO const! or p will be const, cannot assign!
const auto cmp = [](ListNode* a, ListNode* b) { return a->val > b->val; };
std::priority_queue<ListNode* , std::vector<ListNode*>, decltype(cmp)> pq (cmp); // Has to put (cmp)
for (const auto n : lists)
{
if (n)
{
pq.push(n);
}
}
auto p = head;
while (!pq.empty())
{
const auto n = pq.top();
p->next = n;
p = p->next;
pq.pop();
if (n->next)
{
pq.push(n->next);
}
}
return head->next;
}
};