-
Notifications
You must be signed in to change notification settings - Fork 0
/
22. Merge k sorted Lists.cpp
72 lines (59 loc) · 1.82 KB
/
22. 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
vector<ListNode*> NewLists;
ListNode* NewTmp;
int i = 0;
int len = lists.size();
int mid = len%2?len/2:len/2-1;
if(lists.size() == 0)
return NULL;
// Here is a quick tip to merge. Merge the first and the last element and store them into the list. Will result in a total O(n*k*log(k))
//dont go merge the neghiboring lists!!!!!!!
while(len !=1){
for(int i = 0;i<=mid;i++)
if(i != len-i-1){
NewTmp = mergeTwoList(lists[i],lists[len-1-i]);
lists[i] = NewTmp;
}
len = mid+1;
mid = len%2?len/2:len/2-1;
}
return lists[0];
}
ListNode* mergeTwoList(ListNode *l1, ListNode*l2){
ListNode* p1 = l1, *p2 = l2;
ListNode* pPre = NULL;
if(l1 == NULL)
return l2;
while(p1!=NULL&&p2!=NULL){
if(p1->val<p2->val){
pPre = p1;
p1 = p1->next;
}else{
if(pPre != NULL){
pPre->next = p2;
p2=p2->next;
pPre->next->next = p1;
pPre = pPre->next;
}else{
l1 = p2;
p2=p2->next;
pPre = l1;
pPre->next = p1;
}
}
}
if(p2!= NULL)
pPre->next = p2;
return l1;
}
};