-
Notifications
You must be signed in to change notification settings - Fork 0
/
23_merge_k_sorted_list.cpp
52 lines (43 loc) · 1.19 KB
/
23_merge_k_sorted_list.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
#include <vector>
#include <iostream>
using namespace std;
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) {}
};
ListNode* mergeKLists(vector<ListNode*>& lists) {
ListNode* res = new ListNode(0);
ListNode* curr = res;
while (true) {
int min = INT_MAX;
for (auto& ptr : lists) {
if (ptr != NULL && ptr->val < min) min = ptr->val;
}
if (min == INT_MAX) break;
curr->next = new ListNode(min);
curr = curr->next;
for (auto& ptr : lists) {
if (ptr != NULL && ptr->val == min) {
ptr = ptr->next;
break;
}
}
}
return res->next;
}
int main() {
ListNode* a = new ListNode(1);
a->next = new ListNode(4);
a->next->next = new ListNode(5);
ListNode* b = new ListNode(1);
b->next = new ListNode(3);
b->next->next = new ListNode(4);
ListNode* c = new ListNode(2);
c->next = new ListNode(6);
vector<ListNode*> vec = {a, b, c};
ListNode* res = mergeKLists(vec);
return 0;
}