-
Notifications
You must be signed in to change notification settings - Fork 277
/
MergeKSortedListsB.java
73 lines (64 loc) · 1.67 KB
/
MergeKSortedListsB.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
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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if(lists == null ||lists.length ==0) {
return null;
}
return helper(lists, 0, lists.length -1);
}
private ListNode helper(ListNode[] lists, int low, int high){
if(low> high){
return null;
} else if(low == high){
return lists[low];
} else{
int mid = low +(high -low)/2;
ListNode left = helper(lists, low, mid);
ListNode right = helper(lists, mid+1, high);
return merge(left, right);
}
}
private ListNode merge(ListNode l1, ListNode l2){
if(l1 == null){
return l2;
}
if(l2 == null){
return l1;
}
ListNode dummy = new ListNode(-1);
ListNode t= dummy;
ListNode t1= l1;
ListNode t2 = l2;
while(t1!=null && t2!=null){
if(t1.val < t2.val){
t.next = t1;
t = t.next;
t1 = t1.next;
} else{
t.next = t2;
t = t.next;
t2 = t2.next;
}
}
while(t1!=null){
t.next = t1;
t = t.next;
t1 = t1.next;
}
while( t2!=null){
t.next = t2;
t = t.next;
t2 = t2.next;
}
return dummy.next;
}
}