-
Notifications
You must be signed in to change notification settings - Fork 0
/
Merge k Sorted Lists.cpp
58 lines (57 loc) · 1.48 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
49
50
51
52
53
54
55
56
57
58
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l1==NULL)
{
return l2;
}
if(l2==NULL)
{
return l1;
}
ListNode* lNode1=l1;
ListNode* lNode2=l2;
ListNode* newhead;
if(lNode1!=NULL&&lNode2!=NULL)
{
if(lNode1->val>lNode2->val)//l2小,所以头结点
{
newhead=lNode2;
lNode2->next=mergeTwoLists(lNode1,lNode2->next);
}
else//l1小,所以头结点
{
newhead=lNode1;
lNode1->next=mergeTwoLists(lNode1->next,lNode2);
}
}
return newhead;
}
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {//这算是二分法吧
int length=lists.size();
if(length<1)
{
return NULL;
}
int count=1;
while(length>1)//合并的链表个数要大于1个
{
for(int i=0;i<lists.size()-count;i=i+count*2)//count是*2增长的,可以将k举例大一点就可以看出来
{
lists[i]=mergeTwoLists(lists[i],lists[i+count]);
}
count=count*2;
length=length/2+length%2;
}
return lists[0];
}
};