当前位置:网站首页>23. 合并K个升序链表-c语言
23. 合并K个升序链表-c语言
2022-07-07 14:15:00 【Mr Gao】
23. 合并K个升序链表-c语言
给你一个链表数组,每个链表都已经按升序排列。
请你将所有链表合并到一个升序链表中,返回合并后的链表。
示例 1:
输入:lists = [[1,4,5],[1,3,4],[2,6]]
输出:[1,1,2,3,4,4,5,6]
解释:链表数组如下:
[
1->4->5,
1->3->4,
2->6
]
将它们合并到一个有序链表中得到。
1->1->2->3->4->4->5->6
示例 2:
输入:lists = []
输出:[]
示例 3:
输入:lists = [[]]
输出:[]
解题代码如下,很不错的题目:
/** * 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;
}
边栏推荐
- What else can an ordinary person do besides working in a factory to make money?
- Enterprise log analysis system elk
- Iptables only allows the specified IP address to access the specified port
- 应用程序和matlab的通信方式
- Wireless sensor networks -- ZigBee and 6LoWPAN
- 【Android -- 数据存储】使用 SQLite 存储数据
- Numpy -- epidemic data analysis case
- Particle effect for ugui
- 统计学习方法——感知机
- Markdown formula editing tutorial
猜你喜欢

AE learning 02: timeline

【Android -- 数据存储】使用 SQLite 存储数据

喜讯!科蓝SUNDB数据库与鸿数科技隐私数据保护管理软件完成兼容性适配

PyTorch 中的乘法:mul()、multiply()、matmul()、mm()、mv()、dot()

2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo

Strengthen real-time data management, and the British software helps the security construction of the medical insurance platform

Unity3D_ Class fishing project, control the distance between collision walls to adapt to different models

Sysom case analysis: where is the missing memory| Dragon lizard Technology

过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?

Eye of depth (VII) -- Elementary Transformation of matrix (attachment: explanation of some mathematical models)
随机推荐
Eye of depth (VI) -- inverse of matrix (attachment: some ideas of logistic model)
Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?
Use moviepy Editor clips videos and intercepts video clips in batches
Unity drawing plug-in = = [support the update of the original atlas]
Unity3d click events added to 3D objects in the scene
统计学习方法——感知机
分步式监控平台zabbix
Common training data set formats for target tracking
星瑞格数据库入围“2021年度福建省信息技术应用创新典型解决方案”
laravel怎么获取到public路径
Laravel service provider instance tutorial - create a service provider test instance
Logback日志框架第三方jar包 免费获取
Statistical learning method -- perceptron
分类模型评价标准(performance measure)
How to determine whether the checkbox in JS is selected
Introduction to pyGame games
Numpy --- basic learning notes
MySQL中, 如何查询某一天, 某一月, 某一年的数据
torch. Numel action
Laravel constructor and middleware execution order