-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMergekSortedLists23.java
More file actions
65 lines (57 loc) · 1.69 KB
/
MergekSortedLists23.java
File metadata and controls
65 lines (57 loc) · 1.69 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
import java.util.*;
public class MergekSortedLists23 {
public ListNode mergeKLists(ListNode[] lists) {
if (lists.length == 0) {
return null;
}
ArrayList<Integer> arr = new ArrayList<>();
for (ListNode ptr : lists) {
while (ptr != null) {
arr.add(ptr.val);
ptr = ptr.next;
}
}
Collections.sort(arr);
ListNode head = new ListNode(0);
ListNode ptr = head;
for (int x : arr) {
ptr.next = new ListNode(x);
ptr = ptr.next;
}
return head.next;
}
public ListNode mergeLists(ListNode[] lists) {
if (lists == null || lists.length == 0) {
return null;
}
return Helper(lists, 0, lists.length - 1);
}
public ListNode Helper(ListNode[] lists, int start, int end) {
if (start == end) {
return lists[start];
}
if (start + 1 == end) {
return merge(lists[start], lists[end]);
}
int mid = start + (end - start) / 2;
ListNode left = Helper(lists, start, mid);
ListNode right = Helper(lists, mid + 1, end);
return merge(left, right);
}
private ListNode merge(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0);
ListNode curr = dummy;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
curr.next = l1;
l1 = l1.next;
} else {
curr.next = l2;
l2 = l2.next;
}
curr = curr.next;
}
curr.next = (l1 != null) ? l1 : l2;
return dummy.next;
}
}