-
Notifications
You must be signed in to change notification settings - Fork 0
/
Merge K Sorted Lists.cpp
39 lines (35 loc) · 1007 Bytes
/
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
#include <iostream>
#include <queue>
#include <vector>
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution {
public:
ListNode *mergeKLists(std::vector<ListNode *> &lists) {
auto cmp = [&](int x, int y) { return lists[x]->val > lists[y]->val; };
std::priority_queue<int, std::vector<int>, decltype(cmp)> queue(cmp);
for (int i = 0; i < lists.size(); ++i) {
if (lists[i]) {
queue.push(i);
}
}
ListNode dummy;
ListNode *cur = &dummy;
while (!queue.empty()) {
int n = queue.top();
queue.pop();
cur->next = lists[n];
lists[n] = lists[n]->next;
cur = cur->next;
if (lists[n]) {
queue.push(n);
}
}
return dummy.next;
}
};