-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23.merge-k-sorted-lists.cpp
72 lines (69 loc) · 1.38 KB
/
23.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
/*
* [23] Merge k Sorted Lists
*
* https://leetcode.com/problems/merge-k-sorted-lists/description/
*
* algorithms
* Hard (28.31%)
* Total Accepted: 222.4K
* Total Submissions: 785K
* Testcase Example: '[[1,4,5],[1,3,4],[2,6]]'
*
* Merge k sorted linked lists and return it as one sorted list. Analyze and
* describe its complexity.
*
* Example:
*
*
* Input:
* [
* 1->4->5,
* 1->3->4,
* 2->6
* ]
* Output: 1->1->2->3->4->4->5->6
*
*
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
//方法1:两两折半查找
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
int size = lists.size();
if (size == 0 ) return NULL;
while(size > 1){
int next_size = (size + 1) / 2;
for (int i = 0; i < size / 2; ++i){
lists[i] = mergeLists(lists[i], lists[i + next_size]);
}
size = next_size;
}
return lists[0];
}
//merge two list
ListNode* mergeLists(ListNode *l1, ListNode *l2){
ListNode *head = new ListNode(0);
ListNode *cur = head;
while (l1 && l2){
if (l1 -> val < l2 -> val){
cur -> next = new ListNode(l1 -> val);
l1 = l1 -> next;
}
else{
cur -> next = new ListNode(l2 -> val);
l2 = l2 -> next;
}
cur = cur -> next;
}
cur -> next = l1 ? l1 : l2;
return head->next;
}
};