-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeKSortedList.cpp
More file actions
90 lines (84 loc) · 1.91 KB
/
Copy pathMergeKSortedList.cpp
File metadata and controls
90 lines (84 loc) · 1.91 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* Definition for singly-linked list.
*/
#include <vector>
#include <algorithm>
#include <queue>
#include <stdio.h>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
struct cmp {
bool operator()(ListNode* a, ListNode* b) {
return a->val > b->val;
}
};
class Solution {
public:
ListNode *mergeKLists(vector<ListNode *> &lists) {
priority_queue<ListNode*, vector<ListNode*>, cmp> pq;
for (int i = 0; i < lists.size(); i++) {
if (lists[i] != NULL) {
pq.push(lists[i]);
}
}
ListNode *head = NULL;
ListNode *prev = NULL;
while (!pq.empty()) {
ListNode* p = pq.top();
if (head == NULL) {
head = p;
} else {
prev->next = p;
}
pq.pop();
prev = p;
if (p->next != NULL) {
pq.push(p->next);
}
}
return head;
}
};
int main() {
int a[5] = {1, 3, 7, 9, 9};
int b[3] = {2, 4, 5};
ListNode *head1 = NULL;
ListNode *prev = NULL;
for (int i = 0; i < 5; i++) {
ListNode *p = new ListNode(a[i]);
if (i == 0) {
head1 = p;
} else {
prev->next = p;
}
prev = p;
}
ListNode *head2 = NULL;
prev = NULL;
for (int j = 0; j < 3; j++) {
ListNode *p = new ListNode(b[j]);
if (j == 0) {
head2 = p;
} else {
prev->next = p;
}
prev = p;
}
vector<ListNode*> lists;
lists.push_back(head1);
lists.push_back(head2);
Solution s;
ListNode *res = s.mergeKLists(lists);
ListNode *p = res;
while (p!=NULL) {
printf("%d->", p->val);
ListNode *temp = p;
p = p->next;
delete temp;
}
printf("\n");
}