-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeArrary.java
More file actions
59 lines (48 loc) · 1.35 KB
/
Copy pathMergeArrary.java
File metadata and controls
59 lines (48 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
58
59
/*
23. Merge k Sorted Lists
Hard
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
Example:
Input:
[
1->4->5,
1->3->4,
2->6
]
Output: 1->1->2->3->4->4->5->6
解题思路:
解法一:分治,每次对两个链表merge
解法二:最小堆,堆的开始存入每个链表的第一个节点,接下来每次堆顶出堆,然后把该元素对应的链表的下一个节点入堆
*/
package heap;
import linklist.ListNode;
import java.util.Comparator;
import java.util.PriorityQueue;
public class MergeArrary {
public static ListNode mergeKLists(ListNode[] lists) {
if (lists == null) {
return null;
}
if (lists.length == 1) {
return lists[0];
}
PriorityQueue<ListNode> heap = new PriorityQueue<>(new Comparator<ListNode>() {
@Override
public int compare(ListNode o1, ListNode o2) {
return o1.val - o2.val;
}
});
for (int i = 0; i < lists.length; i++) {
heap.add(lists[i]);
}
ListNode pre = new ListNode(0);
while (!heap.isEmpty()) {
ListNode node = heap.poll();
if (node.next != null) {
heap.add(node.next);
}
pre.next = node;
}
return pre.next;
}
}