当前位置:网站首页>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;
}
边栏推荐
- Win11U盘不显示怎么办?Win11插U盘没反应的解决方法
- Reinforcement learning - learning notes 8 | Q-learning
- Index summary (assault version)
- Actual combat: sqlserver 2008 Extended event XML is converted to standard table format [easy to understand]
- Solve the problem of uni in uni app Request sent a post request without response.
- Use json Stringify() to realize deep copy, be careful, there may be a huge hole
- PKPM 2020软件安装包下载及安装教程
- #DAYU200体验官#MPPT光伏发电项目 DAYU200、Hi3861、华为云IotDA
- Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?
- DBSync新增对MongoDB、ES的支持
猜你喜欢
Ternary expressions, generative expressions, anonymous functions
Automatic classification of defective photovoltaic module cells in electronic images
QT compile IOT management platform 39 alarm linkage
反爬通杀神器
operator
Wechat official account oauth2.0 authorizes login and displays user information
PDF文档签名指南
Crawler (17) - Interview (2) | crawler interview question bank
Navicat connect 2002 - can't connect to local MySQL server through socket '/var/lib/mysql/mysql Sock 'solve
Jenkins user rights management
随机推荐
Index summary (assault version)
The strongest installation of the twin tower model, Google is playing "antique" again?
The function is really powerful!
The difference between NPM uninstall and RM direct deletion
Jerry's manual matching method [chapter]
Using enumeration to realize English to braille
Oracle advanced (VI) Oracle expdp/impdp details
Codemail auto collation code of visual studio plug-in
Cv2.resize function reports an error: error: (-215:assertion failed) func= 0 in function ‘cv::hal::resize‘
用语雀写文章了,功能真心强大!
ByteDance Android interview, summary of knowledge points + analysis of interview questions
Devil daddy A0 English zero foundation self-improvement Road
Leetcode SQL first day
Backup tidb cluster to persistent volume
PKPM 2020软件安装包下载及安装教程
Contour layout of margin
Kirin Xin'an operating system derivative solution | storage multipath management system, effectively improving the reliability of data transmission
[开源] .Net ORM 访问 Firebird 数据库
Which financial products will yield high returns in 2022?
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?