题目描述
给你一个链表数组,每个链表都已经按升序排列。请你将所有链表合并到一个升序链表中,返回合并后的链表。示例 1:输入:lists = [[1,4,5],[1,3,4],[2,6]]
输出:[1,1,2,3,4,4,5,6]
解释:链表数组如下:
[1->4->5,1->3->4,2->6
]
将它们合并到一个有序链表中得到。
1->1->2->3->4->4->5->6
示例 2:输入:lists = []
输出:[]
示例 3:输入:lists = [[]]
输出:[]提示:k == lists.length
0 <= k <= 10^4
0 <= lists[i].length <= 500
-10^4 <= lists[i][j] <= 10^4
lists[i] 按 升序 排列
lists[i].length 的总和不超过 10^4来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/merge-k-sorted-lists
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
方法1:顺序合并
思路
跟 21.合并两个有序链表 的基本思路是一样的,只不过每轮操作需要比较 k 个链表节点的值。
复杂度分析
- 时间复杂度:$O(k*n)$,k 是链表个数,n 是合并后链表的长度。
- 空间复杂度:$O(1)$。
代码(JavaScript/C++)
JavaScript Code
/*** Definition for singly-linked list.* function ListNode(val, next) {* this.val = (val===undefined ? 0 : val)* this.next = (next===undefined ? null : next)* }*/ /*** @param {ListNode[]} lists* @return {ListNode}*/ var mergeKLists = function (lists) {if (!lists || !lists.length) return null;let dummy = new ListNode();let tail = dummy;while (!isEmpty(lists)) {const min = getMin(lists);tail.next = new ListNode(min.val);tail = tail.next;}return dummy.next;// **********************************************function getMin(lists) {let minIndex = -1,minNode = new ListNode(Infinity);for (let i = 0; i < lists.length; i++) {const node = lists[i];if (node && node.val < minNode.val) {minNode = node;minIndex = i;}}lists[minIndex] = minNode.next;return minNode;}function isEmpty(lists) {return lists.every(n => n === null);} };
C++ code
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode() : val(0), next(nullptr) {}* ListNode(int x) : val(x), next(nullptr) {}* ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/ class Solution { private:bool isEmpty_(vector<ListNode*>& lists) {for (int i = 0; i < lists.size(); i++) {if (lists[i]) return false;}return true;}int getMinVal_(vector<ListNode*>& lists) {int ans = INT_MAX;int idx = -1;for (int i = 0; i < lists.size(); i++) {if (lists[i] && lists[i]->val < ans) {ans = lists[i]->val;idx = i;}}lists[idx] = lists[idx]->next;return ans;}public:ListNode* mergeKLists(vector<ListNode*>& lists) {ListNode* dummy = new ListNode();ListNode* tail = dummy;while (!isEmpty_(lists)) {int min_val = getMinVal_(lists);tail->next = new ListNode(min_val);tail = tail->next;}return dummy->next;} };
方法2:顺序合并+堆
思路
跟 方法1 思路一致,不过用了堆来寻找 k 个链表节点中的最小值。
复杂度分析
- 时间复杂度:$O(nlogk)$,k 是链表个数,n 是合并后链表的长度。
- 空间复杂度:$O(k)$,k 是链表个数,堆的空间大小。
代码
JavaScript Code
/*** Definition for singly-linked list.* function ListNode(val, next) {* this.val = (val===undefined ? 0 : val)* this.next = (next===undefined ? null : next)* }*/ /*** @param {ListNode[]} lists* @return {ListNode}*/ var mergeKLists = function (lists) {if (!lists || !lists.length) return null;let dummy = new ListNode();let tail = dummy;const heap = new MinHeap(lists.filter(Boolean), function comparator(inserted, compared) {return inserted.val > compared.val;});while (heap.size() > 0) {const min = heap.pop();tail.next = new ListNode(min.val);tail = tail.next;min.next && heap.insert(min.next)}return dummy.next; };// **************************************************class Heap {constructor(list = [], comparator) {this.list = list;this.comparator = comparator;this.init();}init() {const size = this.size();for (let i = Math.floor(size / 2) - 1; i >= 0; i--) {this.heapify(this.list, size, i);}}insert(n) {this.list.push(n);const size = this.size();for (let i = Math.floor(size / 2) - 1; i >= 0; i--) {this.heapify(this.list, size, i);}}peek() {return this.list[0];}pop() {const last = this.list.pop();if (this.size() === 0) return last;const returnItem = this.list[0];this.list[0] = last;this.heapify(this.list, this.size(), 0);return returnItem;}size() {return this.list.length;} }class MinHeap extends Heap {constructor(list, comparator) {if (typeof comparator != 'function') {comparator = function comparator(inserted, compared) {return inserted > compared;};}super(list, comparator);}heapify(arr, size, i) {let smallest = i;const left = Math.floor(i * 2 + 1);const right = Math.floor(i * 2 + 2);if (left < size && this.comparator(arr[smallest], arr[left]))smallest = left;if (right < size && this.comparator(arr[smallest], arr[right]))smallest = right;if (smallest !== i) {[arr[smallest], arr[i]] = [arr[i], arr[smallest]];this.heapify(arr, size, smallest);}} }
方法3:分治
思路
- 将 k 个链表平均分成两份,分别进行合并后,再将两个结果进行合并。
- 最后一步就是简单的 合并两个有序链表。
- 而中间的步骤也很简单,只需要将 k/2 个链表再细分,再细分,细分到一次只需要处理两个链表就好了。
复杂度分析
- 时间复杂度:$O(nlogk)$,k 是链表个数,n 是合并后链表的长度。
- 空间复杂度:$O(k)$,k 是链表个数,堆的空间大小。
代码(JavaScript/C++)
JavaScript Code
/*** Definition for singly-linked list.* function ListNode(val, next) {* this.val = (val===undefined ? 0 : val)* this.next = (next===undefined ? null : next)* }*/ /*** @param {ListNode[]} lists* @return {ListNode}*/ var mergeKLists = function(lists, start = 0, end = lists.length - 1) {if (start > end) return null;if (start === end) return lists[start];const mid = ((end - start) >> 1) + start;return mergeTwoLists(mergeKLists(lists, start, mid), mergeKLists(lists, mid + 1, end)); };function mergeTwoLists(l1, l2) {if (!l1) return l2;if (!l2) return l1;if (l1.val < l2.val) {l1.next = mergeTwoLists(l1.next, l2);return l1;} else {l2.next = mergeTwoLists(l1, l2.next);return l2;} }
C++ Code
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode() : val(0), next(nullptr) {}* ListNode(int x) : val(x), next(nullptr) {}* ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/ class Solution { public:ListNode* mergeKLists(vector<ListNode*>& lists) {return mergeKLists(lists, 0, lists.size() - 1);}ListNode* mergeKLists(vector<ListNode*>& lists, int start, int end) {if (start > end) return nullptr;if (start == end) return lists[start];int m = (end - start) / 2 + start;return merge2Lists(mergeKLists(lists, start, m), mergeKLists(lists, m + 1, end));}ListNode* merge2Lists(ListNode* l1, ListNode* l2) {if (!l1) return l2;if (!l2) return l1;if (l1->val < l2->val) {l1->next = merge2Lists(l1->next, l2);return l1;} else {l2->next = merge2Lists(l1, l2->next);return l2;}} };