LeetCode - 23. Merge k Sorted Lists
23. Merge k Sorted Lists #
Problem's Link
#
----------------------------------------------------------------------------
Mean:
将k个有序链表合并为一个有序链表.
analyse:
方法一:本着不重复发明轮子的原则,使用两两合并,就可以利用前面已经做过的Merge two Sorted Lists.
方法二:抖机灵.将所有结点的val都push_back在一个vector容器中,排序后又重新构造链表.
Time complexity: O(N)
view code
方法一:
ListNode *mergeKLists(vector<ListNode *> &lists) {
if(lists.empty()){
return nullptr;
}
while(lists.size() > 1){
lists.push_back(mergeTwoLists(lists[0], lists[1]));
lists.erase(lists.begin());
lists.erase(lists.begin());
}
return lists.front();
}
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
if(l1 == nullptr){
return l2;
}
if(l2 == nullptr){
return l1;
}
if(l1->val <= l2->val){
l1->next = mergeTwoLists(l1->next, l2);
return l1;
}
else{
l2->next = mergeTwoLists(l1, l2->next);
return l2;
}
}
if(lists.empty()){
return nullptr;
}
while(lists.size() > 1){
lists.push_back(mergeTwoLists(lists[0], lists[1]));
lists.erase(lists.begin());
lists.erase(lists.begin());
}
return lists.front();
}
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
if(l1 == nullptr){
return l2;
}
if(l2 == nullptr){
return l1;
}
if(l1->val <= l2->val){
l1->next = mergeTwoLists(l1->next, l2);
return l1;
}
else{
l2->next = mergeTwoLists(l1, l2->next);
return l2;
}
}
方法二:
/**
* -----------------------------------------------------------------
* Copyright (c) 2016 crazyacking.All rights reserved.
* -----------------------------------------------------------------
* Author: crazyacking
* Date : 2016-02-18-09.02
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long(LL);
typedef unsigned long long(ULL);
const double eps(1e-8);
// Definition for singly-linked list.
struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution
{
public:
ListNode* mergeKLists(vector<ListNode*>& lists)
{
vector<int> nodeVal;
for(auto ptr:lists)
{
while(ptr)
{
nodeVal.push_back(ptr->val);
ptr=ptr->next;
}
}
if(nodeVal.size()<=0)
{
return nullptr;
}
sort(nodeVal.begin(),nodeVal.end());
bool isFirst=1;
ListNode *res=nullptr,*head=nullptr;
for(auto p:nodeVal)
{
if(isFirst)
{
isFirst=0;
head=new ListNode(p);
res=head;
}
else
{
head->next=new ListNode(p);
head=head->next;
}
}
return res;
}
};
ListNode* getList(vector<int>& arr)
{
bool isFirst=1;
ListNode *res=nullptr,*head=nullptr;
for(auto p:arr)
{
if(isFirst)
{
isFirst=0;
head=new ListNode(p);
res=head;
}
else
{
head->next=new ListNode(p);
head=head->next;
}
}
return res;
}
int main()
{
Solution solution;
int n;
while(cin>>n)
{
vector<ListNode*> listVe;
while(n--)
{
int num;
cin>>num;
vector<int> ve;
for(int i=0;i<num;++i)
{
int tmp;
cin>>tmp;
ve.push_back(tmp);
}
listVe.push_back(getList(ve));
}
ListNode* head=solution.mergeKLists(listVe);
while(head)
{
cout<<head->val<<" ";
head=head->next;
}
cout<<"End."<<endl;
}
return 0;
}
/*
*/
* -----------------------------------------------------------------
* Copyright (c) 2016 crazyacking.All rights reserved.
* -----------------------------------------------------------------
* Author: crazyacking
* Date : 2016-02-18-09.02
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long(LL);
typedef unsigned long long(ULL);
const double eps(1e-8);
// Definition for singly-linked list.
struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution
{
public:
ListNode* mergeKLists(vector<ListNode*>& lists)
{
vector<int> nodeVal;
for(auto ptr:lists)
{
while(ptr)
{
nodeVal.push_back(ptr->val);
ptr=ptr->next;
}
}
if(nodeVal.size()<=0)
{
return nullptr;
}
sort(nodeVal.begin(),nodeVal.end());
bool isFirst=1;
ListNode *res=nullptr,*head=nullptr;
for(auto p:nodeVal)
{
if(isFirst)
{
isFirst=0;
head=new ListNode(p);
res=head;
}
else
{
head->next=new ListNode(p);
head=head->next;
}
}
return res;
}
};
ListNode* getList(vector<int>& arr)
{
bool isFirst=1;
ListNode *res=nullptr,*head=nullptr;
for(auto p:arr)
{
if(isFirst)
{
isFirst=0;
head=new ListNode(p);
res=head;
}
else
{
head->next=new ListNode(p);
head=head->next;
}
}
return res;
}
int main()
{
Solution solution;
int n;
while(cin>>n)
{
vector<ListNode*> listVe;
while(n--)
{
int num;
cin>>num;
vector<int> ve;
for(int i=0;i<num;++i)
{
int tmp;
cin>>tmp;
ve.push_back(tmp);
}
listVe.push_back(getList(ve));
}
ListNode* head=solution.mergeKLists(listVe);
while(head)
{
cout<<head->val<<" ";
head=head->next;
}
cout<<"End."<<endl;
}
return 0;
}
/*
*/
作者:北岛知寒
出处:https://www.cnblogs.com/crazyacking/p/5200073.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
分类:
ACM/LC
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?