当前位置:网站首页>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;
}
边栏推荐
- You Yuxi, coming!
- PHP中exit,exit(0),exit(1),exit(‘0’),exit(‘1’),die,return的区别
- 预测——灰色预测
- Leetcode-136- number that appears only once (solve with XOR)
- Odoo integrated plausible embedded code monitoring platform
- Unity drawing plug-in = = [support the update of the original atlas]
- Bidding announcement: Fujian Rural Credit Union database audit system procurement project (re bidding)
- 应用程序和matlab的通信方式
- PHP has its own filtering and escape functions
- Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?
猜你喜欢
TiDB For PostgreSQL和YugabyteDB在Sysbench上的性能对比
spark调优(三):持久化减少二次查询
Plate - forme de surveillance par étapes zabbix
Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!
统计学习方法——感知机
Numpy --- basic learning notes
Three. JS introductory learning notes 19: how to import FBX static model
【C 语言】 题集 of Ⅹ
[flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
What about the pointer in neural network C language
随机推荐
Laravel post shows an exception when submitting data
Bidding announcement: Fujian Rural Credit Union database audit system procurement project (re bidding)
spark调优(三):持久化减少二次查询
过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?
PHP中exit,exit(0),exit(1),exit(‘0’),exit(‘1’),die,return的区别
IP地址和物理地址有什么区别
Unity3d click events added to 3D objects in the scene
95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
PHP has its own filtering and escape functions
The inevitable trend of the intelligent development of ankerui power grid is that microcomputer protection devices are used in power systems
Rongyun won the 2022 China Xinchuang digital office portal excellence product award!
Unity的三种单例模式(饿汉,懒汉,MonoBehaviour)
Tragedy caused by deleting the console statement
hellogolang
121. The best time to buy and sell stocks
asyncio 概念和用法
【Android -- 数据存储】使用 SQLite 存储数据
How to determine whether the checkbox in JS is selected
leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
Particle effect for ugui