题目
思路
- 如何高效地选择最小节点
1.1. 归并排序
1.2. 小顶堆
Java 实现
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if (lists == null || lists.length == 0) {
return null;
}
PriorityQueue<ListNode> pq = new PriorityQueue<>((a, b) -> a.val - b.val);
for (ListNode node : lists) {
if (node != null) {
pq.offer(node);
}
}
ListNode dummy = new ListNode(0), tail = dummy;
while (!pq.isEmpty()) {
ListNode min = pq.poll();
tail.next = min;
tail = min;
if (min.next != null) {
pq.offer(min.next);
}
}
tail.next = null;
return dummy.next;
}
}
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if (lists == null || lists.length == 0) {
return null;
}
return mergeRange(lists, 0, lists.length - 1);
}
private ListNode mergeRange(ListNode[] lists, int l, int r) {
if (l == r) {
return lists[l];
}
int m = l + (r - l) / 2;
ListNode left = mergeRange(lists, l, m);
ListNode right = mergeRange(lists, m + 1, r);
return mergeTwo(left, right);
}
private ListNode mergeTwo(ListNode a, ListNode b) {
ListNode dummy = new ListNode(0), tail = dummy;
while (a != null && b != null) {
if (a.val <= b.val) {
tail.next = a;
a = a.next;
}
else {
tail.next = b;
b = b.next;
}
tail = tail.next;
}
tail.next = (a != null) ? a : b;
return dummy.next;
}
}