-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMergeKSortedLists.java
65 lines (60 loc) · 1.6 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package linked_list;
/**
* @Author: Wenhang Chen
* @Description:合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。 示例:
* <p>
* 输入:
* [
* 1->4->5,
* 1->3->4,
* 2->6
* ]
* 输出: 1->1->2->3->4->4->5->6
* @Date: Created in 16:06 12/19/2019
* @Modified by:
*/
public class MergeKSortedLists {
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
public ListNode mergeKLists(ListNode[] lists) {
if (lists == null || lists.length == 0) return null;
return merge(lists, 0, lists.length - 1);
}
public ListNode merge(ListNode[] lists, int left, int right) {
// 分治
if (left == right) return lists[left];
int mid = (left + right) >>> 1;
// 分成两两链表,下面准备结合
ListNode l1 = merge(lists, left, mid);
ListNode l2 = merge(lists, mid + 1, right);
// 合并
return mergeTwoLists(l1, l2);
}
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode head = new ListNode(0);
ListNode p = head;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
p.next = l1;
p = p.next;
l1 = l1.next;
} else {
p.next = l2;
p = p.next;
l2 = l2.next;
}
}
if (l1 != null) {
p.next = l1;
}
if (l2 != null) {
p.next = l2;
}
return head.next;
}
}