-
-
Notifications
You must be signed in to change notification settings - Fork 610
/
MergekSortedLists.java
47 lines (42 loc) · 1.21 KB
/
MergekSortedLists.java
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
package problems.medium;
import problems.utils.ListNode;
/**
* Created by sherxon on 1/4/17.
*/
public class MergekSortedLists {
public ListNode mergeKLists(ListNode[] a) {
if (a.length == 0) return null;
if(a.length==1)return a[0];
return sort(a, 0, a.length - 1);
}
ListNode sort(ListNode[] a, int lo, int hi) {
if (lo >= hi) return a[lo];
int mid = lo + (hi - lo) / 2;
ListNode left = sort(a, lo, mid);
ListNode right = sort(a, mid + 1, hi);
return merge(left, right);
}
ListNode merge(ListNode left, ListNode right) {
ListNode x= new ListNode(0);
ListNode xx = x;
while (left != null || right != null) {
if (left == null) {
x.next = right;
break;
} else if (right == null) {
x.next = left;
break;
}else{
if (left.val < right.val) {
x.next = left;
left = left.next;
} else {
x.next = right;
right = right.next;
}
}
x=x.next;
}
return xx.next;
}
}