1.题目:合并k个已排序的链表
描述
合并 k 个升序的链表并将结果作为一个升序的链表返回其头节点。
数据范围:节点总数 0 \le n \le 50000≤n≤5000,每个节点的val满足 |val| <= 1000∣val∣<=1000
要求:时间复杂度 O(nlogn)O(nlogn)
示例1
输入:
[{1,2,3},{4,5,6,7}]
返回值:
{1,2,3,4,5,6,7}
示例2
输入:
[{1,2},{1,4,5},{6}]
返回值:
{1,1,2,4,5,6}
2.算法:
1.暴力算法
2.递归算法
3.算法思路
两个两个的合并,最后合成为一个
4.代码:
/*************************************************
作者:She001
时间:2022/9/21
题目:合并k个已排序的链表
描述
合并 k 个升序的链表并将结果作为一个升序的链表返回其头节点。
数据范围:节点总数 0 \le n \le 50000≤n≤5000,每个节点的val满足 |val| <= 1000∣val∣<=1000
要求:时间复杂度 O(nlogn)O(nlogn)
示例1
输入:
[{1,2,3},{4,5,6,7}]
返回值:
{1,2,3,4,5,6,7}
示例2
输入:
[{1,2},{1,4,5},{6}]
返回值:
{1,1,2,4,5,6}
***************************************************/
#include<bits/stdc++.h>
using namespace std;
typedef struct node
{
int i;
node *next;
};
void print(node * head)//打印链表
{
node* pp= head;//复制头节点
while(pp!=NULL)//判断这个节点是否为空 链表是否结束
{
cout<<pp->i<<" ";
pp=pp->next;//指向下一个
}
cout<<endl;
}
//链表的链接
node * lianjie_1(node *a1 , node * b1)// a1 第一个链表的头节点, b1 第二个链表的头节点
{
node * head;//记录头节点
if(a1 == NULL || b1 == NULL)
{
return a1 != NULL ? a1 : b1;
}
node * kk;//连接节点
node* h1=a1;
node * h2=b1;
if((a1->i) < (b1->i))//找出最开始的头节点
{
head = a1;
h1=h1->next;
}
else
{
head = b1;
h2=h2->next;
}
kk=head;//开始连接其余的节点
while(h1!=NULL && h2!=NULL)
{
if((h1->i) < (h2->i))//数值大小比较
{
kk->next=h1;//链表的连接
h1=h1->next;//变成下一个数据
kk=kk->next;//指向下一个节点,
}
else
{
kk->next=h2;
h2=h2->next;
kk=kk->next;
}
}
if(h1==NULL)//还需要尾部的连接
{
kk->next=h2;
}
if(h2==NULL)
{
kk->next=h1;
}
return head;
}
//算法递归 的方法解决两个两个链表的合并
node * lianjie_2(node * list1,node * list2) {
// list1 list2为空的情况
if(list1 == NULL || list2 == NULL ){
return list1 != NULL ? list1 : list2;
}
// 两个链表元素依次对比
if(list1->i <= list2->i ){
// 递归计算 list1.next, list2
list1->next = lianjie_2(list1->next, list2);
return list1;
}else{
// 递归计算 list1, list2.next
list2->next = lianjie_2(list1, list2->next);
return list2;
}
}
node *fangfa_1(vector<node *> &list)
{
vector<node* >::iterator it = list.begin();
node* kk=*(list.begin());//获取第一个链表的头节点 也是返回结果的指针
for(it=list.begin()+1;it!=list.end();it++)
{
kk=lianjie_1(kk,*(it));
}
return kk;
}
int main()
{
//建立一个容器 来存储 k 个 已经排序的链表
vector<node *> lists; //这里我们存储三个链表
//建立 第一个 单链表
node *a1=new node;
node *a2=new node;
node *a3=new node;
node *a4=new node;
node *a5=new node;
node *a6=new node;
node *a7=new node;
a1->i=1;//链表节点的复制
a2->i=3;
a3->i=4;
a4->i=7;
a5->i=9;
a6->i=10;
a7->i=15;
a1->next=a2;//链表的连接
a2->next=a3;
a3->next=a4;
a4->next=a5;
a5->next=a6;
a6->next=a7;
a7->next=NULL;
//建立 第二个 单链表
node *b1=new node;
node *b2=new node;
node *b3=new node;
node *b4=new node;
node *b5=new node;
node *b6=new node;
node *b7=new node;
b1->i=2;//链表节点的复制
b2->i=5;
b3->i=6;
b4->i=8;
b5->i=10;
b6->i=11;
b7->i=20;
b1->next=b2;//链表的连接
b2->next=b3;
b3->next=b4;
b4->next=b5;
b5->next=b6;
b6->next=b7;
b7->next=NULL;
//建立 第三个 单链表
node *c1=new node;
node *c2=new node;
node *c3=new node;
node *c4=new node;
node *c5=new node;
node *c6=new node;
node *c7=new node;
c1->i=4;//链表节点的复制
c2->i=8;
c3->i=12;
c4->i=16;
c5->i=17;
c6->i=18;
c7->i=29;
c1->next=c2;//链表的连接
c2->next=c3;
c3->next=c4;
c4->next=c5;
c5->next=c6;
c6->next=c7;
c7->next=NULL;
lists.insert(lists.end(),a1);//把 a1 这个链表的头节点添加进去
lists.insert(lists.end(),b1);//把 b1 这个链表的头节点添加进去
lists.insert(lists.end(),c1);//把 c1 这个链表的头节点添加进去
//1 3 4 7 9 10 15
//2 5 6 8 10 11 20
//4 8 12 16 17 18 29
node * gg=fangfa_1(lists);
print(gg);
return 0;
}