当前位置:网站首页>23. Merge K ascending linked lists -c language
23. Merge K ascending linked lists -c language
2022-07-07 22:03:00 【Mr Gao】
23. Merge K An ascending list -c Language
Here's an array of linked lists , Each list has been listed in ascending order .
Please merge all the linked lists into one ascending list , Return the merged linked list .
Example 1:
Input :lists = [[1,4,5],[1,3,4],[2,6]]
Output :[1,1,2,3,4,4,5,6]
explain : The linked list array is as follows :
[
1->4->5,
1->3->4,
2->6
]
Combine them into an ordered list to get .
1->1->2->3->4->4->5->6
Example 2:
Input :lists = []
Output :[]
Example 3:
Input :lists = [[]]
Output :[]
The solution code is as follows , Very good topic :
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */
struct ListNode* merge(struct ListNode* list1,struct ListNode* list2){
struct ListNode*p=(struct ListNode*)malloc(sizeof(struct ListNode));
p->next=NULL;
struct ListNode *s=p;
if(list1==NULL&&list2){
p->next=list2;
}
if(list2==NULL&&list1){
p->next=list1;
}
while(list1&&list2){
if(list1->val<list2->val){
p->next=list1;
p=p->next;
list1=list1->next;
if(list1==NULL){
p->next=list2;
}
}
else{
p->next=list2;
p=p->next;
list2=list2->next;
if(list2==NULL){
p->next=list1;
}
}
}
return s->next;;
}
struct ListNode* mergeKLists(struct ListNode** lists, int listsSize){
int i;
if(listsSize==0){
return NULL;
}
struct ListNode *l=lists[0];
for(i=1;i<listsSize;i++){
l=merge(l,lists[i]);
}
return l;
}
边栏推荐
- 你可曾迷茫?曾经的测试/开发程序员,懵懂的小菜C鸟升级......
- Talk about relational database and serverless
- Jerry's key to initiate pairing [chapter]
- Contour layout of margin
- NVR硬盘录像机通过国标GB28181协议接入EasyCVR,设备通道信息不显示是什么原因?
- What is the reason for the abnormal flow consumption of 4G devices accessing the easygbs platform?
- 谈谈制造企业如何制定敏捷的数字化转型策略
- MIT6.S081-Lab9 FS [2021Fall]
- Take the intersection of two sets
- How can big state-owned banks break the anti fraud dilemma?
猜你喜欢
PKPM 2020软件安装包下载及安装教程
Win11如何解禁键盘?Win11解禁键盘的方法
大数据开源项目,一站式全自动化全生命周期运维管家ChengYing(承影)走向何方?
Validutil, "Rethinking the setting of semi supervised learning on graphs"
L2:ZK-Rollup的现状,前景和痛点
Cv2.resize function reports an error: error: (-215:assertion failed) func= 0 in function ‘cv::hal::resize‘
How to turn on win11 game mode? How to turn on game mode in win11
Use json Stringify() to realize deep copy, be careful, there may be a huge hole
Solve the problem of uni in uni app Request sent a post request without response.
嵌入式开发:如何为项目选择合适的RTOS?
随机推荐
Take the intersection of two sets
EasyUI date control emptying value
Focusing on safety in 1995, Volvo will focus on safety in the field of intelligent driving and electrification in the future
嵌入式开发:如何为项目选择合适的RTOS?
Restapi version control strategy [eolink translation]
建立自己的网站(18)
Jenkins user rights management
【JDBC Part 1】概述、获取连接、CRUD
Navicat connect 2002 - can't connect to local MySQL server through socket '/var/lib/mysql/mysql Sock 'solve
Index summary (assault version)
[advanced MySQL] index details (I): index data page structure
Using enumeration to realize English to braille
Code of "digital image processing principle and Practice (matlab version)" part2[easy to understand]
Reinforcement learning - learning notes 9 | multi step TD target
强化学习-学习笔记9 | Multi-Step-TD-Target
使用 CustomPaint 绘制基本图形
Backup tidb cluster to persistent volume
解决uni-app中uni.request发送POST请求没有反应。
Awk processing JSON processing
npm uninstall和rm直接删除的区别