当前位置:网站首页>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;
}
边栏推荐
- Solve the problem of using uni app mediaerror mediaerror errorcode -5
- Actual combat: sqlserver 2008 Extended event XML is converted to standard table format [easy to understand]
- Which financial products will yield high returns in 2022?
- NVR硬盤錄像機通過國標GB28181協議接入EasyCVR,設備通道信息不顯示是什麼原因?
- Qt编写物联网管理平台39-报警联动
- Devil daddy A0 English zero foundation self-improvement Road
- 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?
- The new version of onespin 360 DV has been released, refreshing the experience of FPGA formal verification function
- Usage of MySQL subquery keywords (exists)
- Tsconfig of typescript TS basics JSON configuration options
猜你喜欢

三元表达式、各生成式、匿名函数

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

Ternary expressions, generative expressions, anonymous functions

你可曾迷茫?曾经的测试/开发程序员,懵懂的小菜C鸟升级......

Build your own website (18)
![[开源] .Net ORM 访问 Firebird 数据库](/img/a2/4eff4f0af53bf3b9839a73019a212f.png)
[开源] .Net ORM 访问 Firebird 数据库

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

Dry goods sharing | devaxpress v22.1 original help document download collection

It's worth seeing. Interview sites and interview skills

Navicat connect 2002 - can't connect to local MySQL server through socket '/var/lib/mysql/mysql Sock 'solve
随机推荐
Restore backup data on persistent volumes
L2:ZK-Rollup的现状,前景和痛点
DBSync新增对MongoDB、ES的支持
Ant destination multiple selection
MIT6.S081-Lab9 FS [2021Fall]
强化学习-学习笔记9 | Multi-Step-TD-Target
Problems encountered in installing mysql8 for Ubuntu and the detailed installation process
Two kinds of updates lost and Solutions
Which futures company is the safest to open a futures account?
Arlo's troubles
使用 CustomPaint 绘制基本图形
Meta force force meta universe system development fossage model
Codemail auto collation code of visual studio plug-in
ByteDance senior engineer interview, easy to get started, fluent
2022 how to evaluate and select low code development platforms?
Code of "digital image processing principle and Practice (matlab version)" part2[easy to understand]
Build your own website (18)
Tcp/ip protocol stack
Reinforcement learning - learning notes 8 | Q-learning
【JDBC Part 1】概述、获取连接、CRUD