当前位置:网站首页>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;
}
边栏推荐
- 为什么Win11不能显示秒数?Win11时间不显示秒怎么解决?
- Which futures company is the safest to open a futures account?
- Devil daddy B1 hearing the last barrier, break through with all his strength
- 【Azure微服务 Service Fabric 】因证书过期导致Service Fabric集群挂掉(升级无法完成,节点不可用)
- #DAYU200体验官#MPPT光伏发电项目 DAYU200、Hi3861、华为云IotDA
- 反爬通杀神器
- QT compile IOT management platform 39 alarm linkage
- Develop those things: go plus c.free to free memory, and what are the reasons for compilation errors?
- L'enregistreur de disque dur NVR est connecté à easycvr par le Protocole GB 28181. Quelle est la raison pour laquelle l'information sur le canal de l'appareil n'est pas affichée?
- [open source] Net ORM accessing Firebird database
猜你喜欢
![Restapi version control strategy [eolink translation]](/img/65/decbc158f467ab8c8923c5947af535.png)
Restapi version control strategy [eolink translation]
![[advanced MySQL] index details (I): index data page structure](/img/e7/fe4591a721a71c3c38d6e4448af6af.png)
[advanced MySQL] index details (I): index data page structure

解决uni-app中uni.request发送POST请求没有反应。

The little money made by the program ape is a P!

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

The strongest installation of the twin tower model, Google is playing "antique" again?

The latest Android interview collection, Android video extraction audio

Debugging and handling the problem of jamming for about 30s during SSH login

强化学习-学习笔记9 | Multi-Step-TD-Target

海外代理推荐
随机推荐
海外代理推荐
Actual combat: sqlserver 2008 Extended event XML is converted to standard table format [easy to understand]
An overview of the latest research progress of "efficient deep segmentation of labels" at Shanghai Jiaotong University, which comprehensively expounds the deep segmentation methods of unsupervised, ro
Use br to back up tidb cluster data to azure blob storage
为什么Win11不能显示秒数?Win11时间不显示秒怎么解决?
Redis - basic use (key, string, list, set, Zset, hash, geo, bitmap, hyperloglog, transaction)
ByteDance senior engineer interview, easy to get started, fluent
Jerry's initiation of ear pairing, reconnection, and opening of discoverable and connectable cyclic functions [chapter]
[开源] .Net ORM 访问 Firebird 数据库
Can I open a stock account directly online now? Is it safe?
Win11如何解禁键盘?Win11解禁键盘的方法
谈谈制造企业如何制定敏捷的数字化转型策略
Jerry's about TWS pairing mode configuration [chapter]
PDF文档签名指南
Have you ever been confused? Once a test / development programmer, ignorant gadget C bird upgrade
Win11时间怎么显示星期几?Win11怎么显示今天周几?
Demon daddy B3 read extensively in a small amount, and completed 20000 vocabulary+
The difference between NPM uninstall and RM direct deletion
Jerry's fast pairing does not support canceling pairing [article]
如何实现横版游戏中角色的移动控制