当前位置:网站首页>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;
}
边栏推荐
- 强化学习-学习笔记9 | Multi-Step-TD-Target
- Backup tidb cluster to persistent volume
- Crawler (17) - Interview (2) | crawler interview question bank
- ByteDance Android interview, summary of knowledge points + analysis of interview questions
- 建立自己的网站(18)
- Devil daddy A0 English zero foundation self-improvement Road
- Two kinds of updates lost and Solutions
- An in-depth understanding of fp/fn/precision/recall
- 谈谈制造企业如何制定敏捷的数字化转型策略
- Meta force force meta universe system development fossage model
猜你喜欢
![Jerry's about TWS channel configuration [chapter]](/img/94/fde5054fc412b786cd9864215e912c.png)
Jerry's about TWS channel configuration [chapter]

null == undefined

L2: current situation, prospects and pain points of ZK Rollup

NVR硬盤錄像機通過國標GB28181協議接入EasyCVR,設備通道信息不顯示是什麼原因?

Pre sale 179000, hengchi 5 can fire? Product power online depends on how it is sold

The latest Android interview collection, Android video extraction audio

建立自己的网站(18)

An in-depth understanding of fp/fn/precision/recall

Ad domain group policy management

How to make agile digital transformation strategy for manufacturing enterprises
随机推荐
Node:504 error reporting
Magic weapon - sensitive file discovery tool
How to turn on win11 game mode? How to turn on game mode in win11
Matplotlib drawing interface settings
648. Word replacement
Restapi version control strategy [eolink translation]
Pre sale 179000, hengchi 5 can fire? Product power online depends on how it is sold
Description of the difference between character varying and character in PostgreSQL database
【Azure微服务 Service Fabric 】因证书过期导致Service Fabric集群挂掉(升级无法完成,节点不可用)
Wechat official account oauth2.0 authorizes login and displays user information
ByteDance Android interview, summary of knowledge points + analysis of interview questions
Demon daddy guide post - simple version
Win11时间怎么显示星期几?Win11怎么显示今天周几?
ByteDance senior engineer interview, easy to get started, fluent
Tsconfig of typescript TS basics JSON configuration options
2022 how to evaluate and select low code development platforms?
Ant destination multiple selection
The maximum number of meetings you can attend [greedy + priority queue]
Ternary expressions, generative expressions, anonymous functions
operator