leetcode链接:23. 合并 K 个升序链表
题目分析



方案一
暴力解决
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
| class Solution { public ListNode mergeKLists(ListNode[] lists) { ListNode head = new ListNode(); ListNode node = head;
int index = -1; int tempNum = Integer.MAX_VALUE;
for (;;){ index = -1; tempNum = Integer.MAX_VALUE; for (int i = 0; i <lists.length; i++) { if (lists[i] != null && lists[i].val < tempNum) { tempNum = lists[i].val; index = i; } } if (index == -1) break; node.next = lists[index]; node = node.next; lists[index] = lists[index].next; } return head.next; } }
|
结果
解答成功:
执行耗时:215 ms,击败了5.06% 的Java用户
内存消耗:42.1 MB,击败了81.03% 的Java用户
分析
时间复杂度:
O( n * 数组中的节点数 )
空间复杂度:
O( 1 )
方案二
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
| class Solution { public ListNode mergeKLists(ListNode[] lists) {
PriorityQueue<Integer[]> queue = new PriorityQueue<>((o1, o2) -> o1[0].compareTo(o2[0])); ListNode head = new ListNode(); ListNode node = head;
for (int i = 0; i < lists.length; i++) { if (lists[i] != null) queue.offer(new Integer[]{lists[i].val, i}); }
while (!queue.isEmpty()) { Integer[] poll = queue.poll();
node.next = lists[poll[1]]; node = node.next; lists[poll[1]] = lists[poll[1]].next;
if (lists[poll[1]] != null) { queue.offer(new Integer[]{lists[poll[1]].val, poll[1]}); } }
return head.next; } }
|
结果
解答成功:
执行耗时:5 ms,击败了39.24% 的Java用户
内存消耗:43.3 MB,击败了5.00% 的Java用户
分析
时间复杂度:
O( 数组中的节点数 )
空间复杂度:
O( n )
官方题解
https://leetcode.cn/problems/merge-k-sorted-lists/solutions/219756/he-bing-kge-pai-xu-lian-biao-by-leetcode-solutio-2/
前置知识:合并两个有序链表

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public ListNode mergeTwoLists(ListNode a, ListNode b) { if (a == null || b == null) { return a != null ? a : b; } ListNode head = new ListNode(0); ListNode tail = head, aPtr = a, bPtr = b; while (aPtr != null && bPtr != null) { if (aPtr.val < bPtr.val) { tail.next = aPtr; aPtr = aPtr.next; } else { tail.next = bPtr; bPtr = bPtr.next; } tail = tail.next; } tail.next = (aPtr != null ? aPtr : bPtr); return head.next; }
作者:力扣官方题解 链接:https: 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
|

方法一:顺序合并

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
| class Solution { public ListNode mergeKLists(ListNode[] lists) { ListNode ans = null; for (int i = 0; i < lists.length; ++i) { ans = mergeTwoLists(ans, lists[i]); } return ans; }
public ListNode mergeTwoLists(ListNode a, ListNode b) { if (a == null || b == null) { return a != null ? a : b; } ListNode head = new ListNode(0); ListNode tail = head, aPtr = a, bPtr = b; while (aPtr != null && bPtr != null) { if (aPtr.val < bPtr.val) { tail.next = aPtr; aPtr = aPtr.next; } else { tail.next = bPtr; bPtr = bPtr.next; } tail = tail.next; } tail.next = (aPtr != null ? aPtr : bPtr); return head.next; } }
作者:力扣官方题解 链接:https: 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
|

方法二:分治合并


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
| class Solution { public ListNode mergeKLists(ListNode[] lists) { return merge(lists, 0, lists.length - 1); }
public 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)); }
public ListNode mergeTwoLists(ListNode a, ListNode b) { if (a == null || b == null) { return a != null ? a : b; } ListNode head = new ListNode(0); ListNode tail = head, aPtr = a, bPtr = b; while (aPtr != null && bPtr != null) { if (aPtr.val < bPtr.val) { tail.next = aPtr; aPtr = aPtr.next; } else { tail.next = bPtr; bPtr = bPtr.next; } tail = tail.next; } tail.next = (aPtr != null ? aPtr : bPtr); return head.next; } }
作者:力扣官方题解 链接:https: 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
|

方法三:使用优先队列合并

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
| class Solution { class Status implements Comparable<Status> { int val; ListNode ptr;
Status(int val, ListNode ptr) { this.val = val; this.ptr = ptr; }
public int compareTo(Status status2) { return this.val - status2.val; } }
PriorityQueue<Status> queue = new PriorityQueue<Status>();
public ListNode mergeKLists(ListNode[] lists) { for (ListNode node: lists) { if (node != null) { queue.offer(new Status(node.val, node)); } } ListNode head = new ListNode(0); ListNode tail = head; while (!queue.isEmpty()) { Status f = queue.poll(); tail.next = f.ptr; tail = tail.next; if (f.ptr.next != null) { queue.offer(new Status(f.ptr.next.val, f.ptr.next)); } } return head.next; } }
作者:力扣官方题解 链接:https: 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
|
