-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergeklists.java
46 lines (45 loc) · 1.3 KB
/
mergeklists.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
import java.util.*;
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/ // 分治法
public class mergeklists {
public ListNode mergeKLists(ArrayList<ListNode> lists) {
if (lists == null || lists.size() == 0)return null;
return mergelists(lists, 0, lists.size() - 1);
}
public ListNode mergelists(ArrayList<ListNode> lists, int l, int f) {
if (l == f) return lists.get(l);
ListNode list1 = mergelists(lists, l,l+(f-l)/2);
ListNode list2 = mergelists(lists, l+1+(f-l)/2, f);
return merge2(list1, list2);
}
public ListNode merge2(ListNode list1, ListNode list2) {
ListNode dummy = new ListNode(-1);
ListNode tail = dummy;
while (list1 != null && list2 != null) {
if (list1.val < list2.val) {
tail.next = list1;
list1 = list1.next;
} else {
tail.next = list2;
list2 = list2.next;
}
tail = tail.next;
}
if (list1 != null) {
tail.next = list1;
}
if (list2 != null) {
tail.next = list2;
}
return dummy.next;
}
}