-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path23.cpp
More file actions
56 lines (52 loc) · 1.98 KB
/
Copy path23.cpp
File metadata and controls
56 lines (52 loc) · 1.98 KB
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
// Overall intuition:
// We will build the list in order. Since the lists are sorted, the next element
// is one of the heads of the lists. The best way to find the minimum among the heads
// is to use a PriorityQueue() implemented using a min-heap.
// Time and space complexity:
// Let n be the number of elements across all lists
// Let k be the number of lists
// Initialization is O(k), pop and push in the core while loop are O(log k)
// The complete while loop and algorithm is O(n log k)
// Space is O(k) for the heap
// custom comparator for a min-heap
struct CompareNode {
bool operator()(ListNode* a, ListNode* b) const {
return a->val > b->val;
}
};
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
std::priority_queue<ListNode*, std::vector<ListNode*>, CompareNode> pq;
// push the head of each non-empty list to the pq
for(ListNode* head : lists) {
if (head != nullptr) {
pq.push(head);
}
}
// create a dummy head to start the merged list
ListNode dummy(0);
ListNode* tail = &dummy;
// Extract the minimum head and append it to the merged list
while (!pq.empty()) {
ListNode* smallest = pq.top();
pq.pop();
// add smallest to the list and move the tail pointer forward
tail->next = smallest;
tail = smallest;
if (smallest->next != nullptr) {
pq.push(smallest->next);
}
}
// the dummy node was not actually part of the merged list
return dummy.next;
}
};
// Definition for singly-linked list.
// 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) {}
// };