forked from rost0413/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge_k_Sorted_Lists.cpp
More file actions
160 lines (144 loc) · 3.89 KB
/
Merge_k_Sorted_Lists.cpp
File metadata and controls
160 lines (144 loc) · 3.89 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
Author: Weixian Zhou, ideazwx@gmail.com
Date: Jul 6, 2012
Problem: Merge k Sorted Lists
Difficulty: medium
Source: http://www.leetcode.com/onlinejudge
Notes:
Merge k sorted linked lists and return it as one sorted list. Analyze and
describe its complexity.
Solution:
Use min heap, the time complexity is O(n*lgk), n is the number of elelments
of all lists.
*/
#include <vector>
#include <set>
#include <climits>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <queue>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
struct Compare {
bool operator ()(const ListNode* a, const ListNode* b) {
return a->val > b->val;
}
};
class Solution {
public:
ListNode *mergeKLists(vector<ListNode *> &lists) {
priority_queue<ListNode *, vector<ListNode *>, Compare> min_heap;
ListNode *head = NULL;
ListNode *tail = NULL;
for (unsigned i = 0; i < lists.size(); i++) {
if (lists[i] != NULL) {
min_heap.push(lists[i]);
}
}
while (!min_heap.empty()) {
ListNode *node = min_heap.top();
min_heap.pop();
if (head == NULL) {
head = node;
tail = node;
} else {
tail->next = node;
tail = node;
}
node = node->next;
if (node != NULL) {
min_heap.push(node);
}
}
return head;
}
};
// pairwise merge, Ke Hu (mrhuke@gmail.com)
class Solution {
public:
ListNode *mergeTwoLists(ListNode *a, ListNode *b)
{
if (!a || !b) return a == NULL ? b : a;
if (a->val < b->val){
ListNode *merged = mergeTwoLists(a->next, b);
a->next = merged;
return a;
}
else{
ListNode *merged = mergeTwoLists(a, b->next);
b->next = merged;
return b;
}
}
ListNode *mergeKLists(vector<ListNode *> &lists) {
if (lists.empty()) return NULL;
int curSize = lists.size();
while(curSize > 1){
int halfSize = curSize/2;
for ( int i = 0; i < halfSize; ++i){
ListNode *a = lists[i];
ListNode *b = lists[i+halfSize+curSize%2];
ListNode *c = mergeTwoLists(a,b);
lists[i] = c;
}
curSize = halfSize + curSize % 2;
}
return lists[0];
}
};
// a merge-sort like idea, mrhuke@gmail.com, may. 13
public class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2)
{
if (list1 == null || list2 == null)
return list1 == null ? list2 : list1;
ListNode newHead = null, cur = null;
while( list1 != null && list2 != null ){
if ( list1.val < list2.val ){
if (newHead == null){
newHead = cur = list1;
}else{
cur.next = list1;
cur = cur.next;
}
list1 = list1.next;
}
else{
if (newHead == null){
newHead = cur = list2;
}else{
cur.next = list2;
cur = cur.next;
}
list2 = list2.next;
}
}
if (list1 != null)
cur.next = list1;
if (list2 != null)
cur.next = list2;
return newHead;
}
public ListNode mergeLists(ArrayList<ListNode> lists, int i, int j)
{
if ( i >= j ) return lists.get(i);
int mid = (i+j)/2;
ListNode list1 = mergeLists(lists, i, mid);
ListNode list2 = mergeLists(lists, mid+1, j);
ListNode merged = mergeTwoLists(list1, list2);
return merged;
}
public ListNode mergeKLists(ArrayList<ListNode> lists) {
if (lists.isEmpty()) return null;
return mergeLists(lists, 0, lists.size()-1);
}
}