-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path23.cpp
More file actions
55 lines (51 loc) · 1.24 KB
/
Copy path23.cpp
File metadata and controls
55 lines (51 loc) · 1.24 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
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
struct CompareListNode {
bool operator()(const ListNode* p, const ListNode* q)const {
return p->val > q->val;
}
};
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
int len = lists.size();
if(len == 0)
return NULL;
ListNode *head = new ListNode(0);
ListNode *p = head;
priority_queue<ListNode*, vector<ListNode*>, CompareListNode> que;
for(int i = 0; i < len; i++) {
if(lists[i])
que.push(lists[i]);
}
while(!que.empty()) {
p->next = que.top();
que.pop();
p = p->next;
if(p->next)
que.push(p->next);
}
p->next = NULL;
return head->next;
}
};
int main() {
ListNode* list = new ListNode(1);
ListNode* list1 = new ListNode(0);
vector<ListNode*> v;
v.push_back(list);
v.push_back(list1);
Solution s;
ListNode *head = s.mergeKLists(v);
while(head != NULL) {
cout << head->val << endl;
head = head->next;
}
}