当前位置:网站首页>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;
}
边栏推荐
- Prometheus remote_ write InfluxDB,unable to parse authentication credentials,authorization failed
- ISO 26262 - considerations other than requirements based testing
- How much does it cost to develop a small program mall?
- Qt编写物联网管理平台39-报警联动
- [colmap] sparse reconstruction is converted to mvsnet format input
- L2: current situation, prospects and pain points of ZK Rollup
- How to integrate Google APIs with Google's application system (1) -introduction to Google APIs
- Paint basic graphics with custompaint
- Jerry's key to initiate pairing [chapter]
- Static test tool
猜你喜欢
双塔模型的最强出装,谷歌又开始玩起“老古董”了?
Open source OA development platform: contract management user manual
PDF文档签名指南
解决uni-app中uni.request发送POST请求没有反应。
TCP/IP 协议栈
L2:ZK-Rollup的现状,前景和痛点
Pre sale 179000, hengchi 5 can fire? Product power online depends on how it is sold
Kirin Xin'an operating system derivative solution | storage multipath management system, effectively improving the reliability of data transmission
Usage of MySQL subquery keywords (exists)
Default constraint and zero fill constraint of MySQL constraint
随机推荐
Google SEO external chain backlinks research tool recommendation
Why can't win11 display seconds? How to solve the problem that win11 time does not display seconds?
Restore backup data on persistent volumes
Take the intersection of two sets
Talk about relational database and serverless
Ternary expressions, generative expressions, anonymous functions
#DAYU200体验官#MPPT光伏发电项目 DAYU200、Hi3861、华为云IotDA
How does win11 time display the day of the week? How does win11 display the day of the week today?
Win11时间怎么显示星期几?Win11怎么显示今天周几?
Reptile combat (VII): pictures of the king of reptiles' heroes
cv2.resize函数报错:error: (-215:Assertion failed) func != 0 in function ‘cv::hal::resize‘
Focusing on safety in 1995, Volvo will focus on safety in the field of intelligent driving and electrification in the future
An in-depth understanding of fp/fn/precision/recall
MIT6.S081-Lab9 FS [2021Fall]
【JDBC Part 1】概述、获取连接、CRUD
Awk processing JSON processing
How to make agile digital transformation strategy for manufacturing enterprises
The strongest installation of the twin tower model, Google is playing "antique" again?
Backup tidb cluster to persistent volume
Which financial products will yield high returns in 2022?