当前位置:网站首页>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;
}
边栏推荐
- The unity vector rotates at a point
- Laravel5.1 路由 -路由分组
- laravel 是怎么做到运行 composer dump-autoload 不清空 classmap 映射关系的呢?
- Multiplication in pytorch: mul (), multiply (), matmul (), mm (), MV (), dot ()
- 统计学习方法——感知机
- Regular expression string
- Migration and reprint
- SysOM 案例解析:消失的内存都去哪了 !| 龙蜥技术
- Laravel post shows an exception when submitting data
- 如何在shell中实现 backspace
猜你喜欢

模仿企业微信会议室选择

Shipping companies' AI products are mature, standardized and applied on a large scale. CIMC, the global leader in port and shipping AI / container AI, has built a benchmark for international shipping

华东师大团队提出,具有DNA调控电路的卷积神经网络的系统分子实现

谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题

Three. JS introductory learning notes 19: how to import FBX static model

You Yuxi, coming!

Continuous creation depends on it!

Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!

How does geojson data merge the boundaries of regions?

Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?
随机推荐
航天宏图信息中标乌鲁木齐某单位数据库系统研发项目
U3D_ Infinite Bessel curve
How does laravel run composer dump autoload without emptying the classmap mapping relationship?
TiDB For PostgreSQL和YugabyteDB在Sysbench上的性能对比
【C 语言】 题集 of Ⅹ
Excessive dependence on subsidies, difficult collection of key customers, and how strong is the potential to reach the dream of "the first share of domestic databases"?
asyncio 概念和用法
Eye of depth (VII) -- Elementary Transformation of matrix (attachment: explanation of some mathematical models)
What about the pointer in neural network C language
js中复选框checkbox如何判定为被选中
Communication mode between application program and MATLAB
Bidding announcement: 2022 Yunnan Unicom gbase database maintenance public comparison and selection project (second) comparison and selection announcement
应用程序和matlab的通信方式
企业级日志分析系统ELK
Numpy -- epidemic data analysis case
laravel中将session由文件保存改为数据库保存
Asyncio concept and usage
2022第四届中国(济南)国际智慧养老产业展览会,山东老博会
hellogolang
PHP realizes wechat applet face recognition and face brushing login function