-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode_00023.java
More file actions
72 lines (67 loc) · 2.1 KB
/
LeetCode_00023.java
File metadata and controls
72 lines (67 loc) · 2.1 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
66
67
68
69
70
71
72
package com.github.jerring.leetcode;
public class LeetCode_00023 {
// // 时间复杂度为 O(nlogk)
// public ListNode mergeKLists(ListNode[] lists) {
// if (lists == null || lists.length == 0) {
// return null;
// }
// PriorityQueue<ListNode> queue = new PriorityQueue<>(lists.length, new Comparator<ListNode>() {
// @Override
// public int compare(ListNode o1, ListNode o2) {
// return Integer.compare(o1.val, o2.val);
// }
// });
// for (ListNode node : lists) {
// if (node != null) {
// queue.offer(node);
// }
// }
// ListNode dummy = new ListNode(-1);
// ListNode cur = dummy;
// while (!queue.isEmpty()) {
// cur.next = queue.poll();
// cur = cur.next;
// if (cur.next != null) {
// queue.offer(cur.next);
// }
// }
// return dummy.next;
// }
// 时间复杂度为 O(nlogk)
public ListNode mergeKLists(ListNode[] lists) {
if (lists == null || lists.length == 0) {
return null;
}
return mergeRangeLists(lists, 0, lists.length - 1);
}
private ListNode mergeRangeLists(ListNode[] lists, int lo, int hi) {
if (lo == hi) {
return lists[lo];
}
int mid = lo + (hi - lo) / 2;
ListNode l1 = mergeRangeLists(lists, lo, mid);
ListNode l2 = mergeRangeLists(lists, mid + 1, hi);
return mergeTwoLists(l1, l2);
}
private ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(-1);
ListNode node = dummy;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
node.next = l1;
l1 = l1.next;
} else {
node.next = l2;
l2 = l2.next;
}
node = node.next;
}
if (l1 != null) {
node.next = l1;
}
if (l2 != null) {
node.next = l2;
}
return dummy.next;
}
}