Repository navigation
Expand file tree
/
Copy path23_Merge_k_Sorted_Lists.java
More file actions
196 lines (170 loc) · 4.76 KB
/
Copy path23_Merge_k_Sorted_Lists.java
File metadata and controls
196 lines (170 loc) · 4.76 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* 23. Merge k Sorted Lists
* Target: Given n pairs of parentheses, generate all combinations of well-formed parentheses.
* Difficulty:Medium
* Classification:String, Backtracking
*/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
/*
* Solution 1
* 2019-06-15 Runtime: 7 ms
* Algorithm: => Brute Force.
* Time complexity: O(n*logn). Space complexity: O(n).
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
List<Integer> nodes = new ArrayList<>();
ListNode head = new ListNode(0);
ListNode point = head;
for (ListNode l : lists){
while (l != null){
nodes.add(l.val);
l = l.next;
}
}
Collections.sort(nodes);
for (int k : nodes) {
point.next = new ListNode(k);
point = point.next;
}
return head.next;
}
}
/*
* Solution 2
* 2019-06-15 Runtime: 322 ms
* Algorithm: => Compare one by one.
* Time complexity: O(k*n). Space complexity: O(1).
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
int minIndex = 0;
ListNode head = new ListNode(0);
ListNode cur = head;
while(true){
boolean isBreak = true;
int minVal = Integer.MAX_VALUE;
for (int i = 0; i < lists.length; i++){
if (lists[i] != null) {
if (lists[i].val < minVal){
minIndex = i;
minVal = lists[i].val;
}
isBreak = false;
}
}
if (isBreak){
break;
}
cur.next = lists[minIndex];
cur = cur.next;
lists[minIndex] = lists[minIndex].next;
}
cur.next = null;
return head.next;
}
}
/*
* Solution 3
* 2019-06-15 Runtime: 5 ms
* Algorithm: => Optimize solution 2 by Priority Queue.
* Time complexity: O(nlog(k)). Space complexity: O(k).
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
Comparator<ListNode> cmp = new Comparator<ListNode>() {
@Override
public int compare(ListNode l1, ListNode l2){
return (l1.val - l2.val);
}
};
Queue<ListNode> q = new PriorityQueue<ListNode>(cmp);
for (ListNode l : lists) {
if (l != null) {
q.add(l);
}
}
ListNode head = new ListNode(0);
ListNode cur = head;
while (!q.isEmpty()){
cur.next = q.poll();
cur = cur.next;
ListNode next = cur.next;
if (next != null){
q.add(next);
}
}
return head.next;
}
}
/*
* Solution 4
* 2019-06-15 Runtime: 302 ms
* Algorithm: => Merge lists one by one.
* Time complexity: O(k*n). Space complexity: O(1).
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 != null && l2 != null){
if (l1.val > l2.val){
ListNode temp = l1;
l1 = l2;
l2 = temp;
}
l1.next = mergeTwoLists(l1.next, l2);
}
return (l1 != null) ? l1 : l2;
}
public ListNode mergeKLists(ListNode[] lists) {
if (lists.length == 0){
return null;
}
if(lists.length == 1){
return lists[0];
}
ListNode head = mergeTwoLists(lists[0], lists[1]);
for (int i = 2; i < lists.length; i++){
head = mergeTwoLists(head, lists[i]);
}
return head;
}
}
/*
* Solution 5
* 2019-06-16 Runtime: 2 ms
* Algorithm: => Merge with Divide And Conquer.
* Time complexity: O(nlog(k)). Space complexity: O(1).
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 != null && l2 != null){
if (l1.val > l2.val){
ListNode temp = l1;
l1 = l2;
l2 = temp;
}
l1.next = mergeTwoLists(l1.next, l2);
}
return (l1 != null) ? l1 : l2;
}
public ListNode mergeKLists(ListNode[] lists) {
if (lists.length == 0){
return null;
}
int point = 1;
while (point < lists.length){
for (int i = 0; i + point < lists.length; i = i + point * 2){
lists[i] = mergeTwoLists(lists[i], lists[i + point]);
}
point *= 2;
}
return lists[0];
}
}