顺序合并
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
ListNode res = null;
for (int i = 0; i < lists.length; i++) {
res = mergeTwoLists(res, lists[i]);
}
return res;
}
private ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0);
ListNode p = dummy;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
p.next = l1;
l1 = l1.next;
} else {
p.next = l2;
l2 = l2.next;
}
p = p.next;
}
p.next = l1 != null ? l1 : l2;
return dummy.next;
}
}
分治合并
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
return merge(lists, 0, lists.length - 1);
}
private ListNode merge(ListNode[] lists, int l, int r) {
if (l == r) {
return lists[l];
}
if (l > r) {
return null;
}
int mid = l + r >> 1;
return mergeTwoLists(merge(lists, l, mid), merge(lists, mid + 1, r));
}
private ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0);
ListNode p = dummy;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
p.next = l1;
l1 = l1.next;
} else {
p.next = l2;
l2 = l2.next;
}
p = p.next;
}
p.next = l1 != null ? l1 : l2;
return dummy.next;
}
}
- 时间复杂度 O(kn * logk)
- 空间复杂度 O(logk)
K个指针比较,选取最小值
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
int k = lists.length;
ListNode dummy = new ListNode(0);
ListNode p = dummy;
while (true) {
// minNode记录k个节点中值最小的节点
ListNode minNode = null;
// minIndex记录最小节点在数组中的索引
int minIndex = -1;
// 比较K个节点
for (int i = 0; i < k; i++) {
if (lists[i] == null) {
continue;
}
if (minNode == null || lists[i].val < minNode.val) {
minNode = lists[i];
minIndex = i;
}
}
// 如果最小节点为空,则说明已经遍历完所有链表
if (minIndex == -1) {
break;
}
// 移动结果链表
p.next = minNode;
p = p.next;
lists[minIndex] = lists[minIndex].next;
}
return dummy.next;
}
}
优先级队列构建最小堆
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
int k = lists.length;
ListNode dummy = new ListNode(0);
ListNode p = dummy;
Queue<ListNode> heap = new PriorityQueue<>((v1, v2) -> v1.val - v2.val);
for (ListNode node: lists) {
if (node != null) {
heap.offer(node);
}
}
while (!heap.isEmpty()) {
ListNode minNode = heap.poll();
p.next = minNode;
p = p.next;
if (minNode.next != null) {
heap.offer(minNode.next);
}
}
return dummy.next;
}
}
- 时间复杂度 O(kn * logk)
- 空间复杂度 O(k)