当前位置:网站首页>力扣23题,合并K个升序链表
力扣23题,合并K个升序链表
2022-06-11 18:03:00 【瀛台夜雪】
力扣23题,合并K个升序链表
题目描述
给你一个链表数组,每个链表都已经按升序排列。请你将所有链表合并到一个升序链表中,返回合并后的链表。
输入输出样例
输入: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
输入:lists = []
输出:[]
输入:lists = [[]]
输出:[]
解法一,(顺序合并)
//合并k个升序链表
ListNode *mergeKLists(vector<ListNode*>&lists)
{
ListNode* resList;
int length=lists.size();
if(length==0)
{
return resList=nullptr;
}
else if(length==1)
{
resList=lists[0];
return resList;
}
resList=lists[0];
//设置标志位
for(int i=1;i<length;i++)
{
resList=mergeList(resList,lists[i]);
}
// resList=mergeList(lists[0],lists[1]);
// cout<<"合并后的list元素: ";
// printList(resList->next);
return resList;
}
ListNode *mergeList(ListNode *list1,ListNode *list2)
{
if(list1==nullptr)
{
return list2;
}
else if(list2==nullptr)
{
return list1;
}
else if(list1->val<list2->val)
{
list1->next=mergeList(list1->next,list2);
return list1;
}
else
{
list2->next=mergeList(list1,list2->next);
return list2;
}
}
采用leetcode23题中的方法,首先对两个链表进行递归合并,然后在推广到三个,四个以及多个链表的合并。
解法二:使用分治算法
ListNode *mergeKList2(vector<ListNode*>&lists)
{
int length=lists.size();
ListNode* resList;
if(length==0)
{
return resList=nullptr;
}
else if(length==1)
{
resList=lists[0];
return resList;
}
int interval=1;
while(interval<length)
{
for(int i=0;i<length-interval;i=i+2*interval)
{
lists[i]=mergeList(lists[i],lists[i+interval]);
}
interval*=2;
}
return lists[0];
}
分治算法相较于顺序合并就是一个逐次进行两两匹配的过程。
边栏推荐
- SISO decoder for a general (n,n-1) SPC code(補充章節3)
- 【C】 Compilation preprocessing and environment
- viso的常见操作
- 软件测试技术复习
- [not forgetting the original intention and forging ahead] 2021 Zhongchuang Suanli new year conference and anniversary celebration
- vulhub
- Sword finger offer (2nd Edition)
- 谈谈远程工作 | 社区征文
- Hello go (XI). Go language common standard library I
- [C语言]限制查找次数,输出次数内查找到的最大值
猜你喜欢
随机推荐
Hwang
Sa-Token 单点登录 SSO模式二 URL重定向传播会话示例
HashSet集合存储学生对象并遍历
HashSet集合
RadioGroup动态添加RadioButton
Mysql8 installation, Navicat installation, sqli labs setup
[c language] output the students with the highest scores with a structure. There can be multiple highest scores
如何学习和自学
Introduction to social engineering practice
SISO Decoder for Repetition(补充章节4)
Global and Chinese market of high frequency bipolar junction transistors 2022-2028: Research Report on technology, participants, trends, market size and share
[c language] output the average score and the student data below or equal to the average score with the structure
Tle6389-2g V50's unique pwm/pfm control scheme has a duty cycle of up to 100%, forming a very low differential pressure - keshijin mall
[C语言]用结构体把平均分和低于等于平均分的学生数据输出
SISO decoder for a general (n,n-1) SPC code(补充章节3)
Upload labs failed to pass the customs halfway and the middle road collapsed
[c language] compress strings and add markup characters
File class learning
Radio button text background changes at the same time
Reading summary of nacos2.x source code
![Codeworks round 479 (Div. 3) [done]](/img/a0/f3c6989d8f755c03076b237514ee64.jpg)







![[untitled]](/img/ab/04beacfc7975cc3c54399447aa5027.png)
