-
Notifications
You must be signed in to change notification settings - Fork 369
/
Merge k Sorted Lists.cpp
48 lines (46 loc) · 1.06 KB
/
Merge k Sorted Lists.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
/*
Example 1:
Input: lists = [[1,4,5],[1,3,4],[2,6]]
Output: [1,1,2,3,4,4,5,6]
Explanation: The linked-lists are:
[
1->4->5,
1->3->4,
2->6
]
merging them into one sorted list:
1->1->2->3->4->4->5->6 */
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) {}
};
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
int k=lists.size();
if(k==0)
return NULL;
vector<pair<int, ListNode*>> a;
for(int i=0;i<k;i++)
{
ListNode* n=lists[i];
while(n!=NULL)
{
a.push_back({n->val,n});
n=n->next;
}
}
if(a.size()==0)
return NULL;
sort(a.begin(),a.end());
for(int i=0;i<a.size()-1;i++)
{
a[i].second->next=a[i+1].second;
}
a[a.size()-1].second->next=NULL;
return a[0].second;
}
};