-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeKLists2.java
More file actions
57 lines (51 loc) · 1.35 KB
/
MergeKLists2.java
File metadata and controls
57 lines (51 loc) · 1.35 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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode mergeKLists(ArrayList<ListNode> lists) {
if(lists == null || lists.size()==0) return null;
int last = lists.size()-1;
while(last>0) {
int begin = 0;
while(begin<last) {
lists.set(begin, hp(lists.get(begin++), lists.get(last--)));
}
}
return lists.get(0);
}
private ListNode hp (ListNode l1 , ListNode l2) {
if(l1==null) return l2;
if(l2==null) return l1;
ListNode head = null;
ListNode curr = null;
if(l1.val<l2.val) {
head = l1;
curr = head;
l1 = l1.next;
} else {
head = l2;
curr = head;
l2 = l2.next;
}
while(l1!=null || l2!=null) {
if(l2 == null || l1!=null && l1.val<l2.val) {
curr.next = l1;
curr = curr.next;
l1 = l1.next;
} else {
curr.next = l2;
curr = curr.next;
l2 = l2.next;
}
}
return head;
}
}