-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path23.cpp
More file actions
73 lines (57 loc) · 1.2 KB
/
Copy path23.cpp
File metadata and controls
73 lines (57 loc) · 1.2 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
#include <vector>
#include <iostream>
#include <queue>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class cmp
{
public:
bool operator() (ListNode* a, ListNode* b)
{
return a->val > b->val;
}
};
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
if(lists.size() == 0)return NULL;
ListNode* ret = NULL, *tail = NULL;
priority_queue<ListNode*, vector<ListNode*>, cmp> q;
for(int i = 0; i < lists.size(); ++i)
{
if(lists[i] == NULL)continue;
q.push(lists[i]);
}
ListNode* t;
while(q.size() > 0)
{
t = q.top();
q.pop();
//cout << t->val << endl;
if(t->next != NULL)
{
q.push(t->next);
}
if(ret == NULL)
{
ret = t;
tail = t;
}
else
{
tail->next = t;
tail = t;
}
}
return ret;
}
};
int main()
{
Solution s;
return 0;
}