一、题目
给你一个链表数组,每个链表都已经按升序排列。
请你将所有链表合并到一个升序链表中,返回合并后的链表。
示例 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
二、题解
思路:遍历+递归,结合 LeetCode 21. 合并两个有序链表_xunye的博客-CSDN博客
/**
* 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) {
if (lists.empty()) return nullptr;
ListNode* p = nullptr;
for (auto list : lists)
{
p = mergeLists(p, list);
}
return p;
}
private:
ListNode* mergeLists(ListNode* l1, ListNode* l2)
{
if (l1 == nullptr and l2 == nullptr) return nullptr;
if (l1 == nullptr and l2 != nullptr) return l2;
if (l1 != nullptr and l2 == nullptr) return l1;
if (l1->val < l2->val)
{
l1->next = mergeLists(l1->next, l2);
return l1;
}
else
{
l2->next = mergeLists(l1, l2->next);
return l2;
}
}
};
简单优化版:
/**
* 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) {
if (lists.empty()) return nullptr;
ListNode* p = lists[0];
auto size = lists.size();
for (decltype(size) i = 1; i < size; ++i)
{
p = mergeLists(p, lists[i]);
}
return p;
}
private:
ListNode* mergeLists(ListNode* l1, ListNode* l2)
{
if (l1 == nullptr and l2 == nullptr) return nullptr;
if (l1 == nullptr and l2 != nullptr) return l2;
if (l1 != nullptr and l2 == nullptr) return l1;
if (l1->val < l2->val)
{
l1->next = mergeLists(l1->next, l2);
return l1;
}
else
{
l2->next = mergeLists(l1, l2->next);
return l2;
}
}
};