当前位置:网站首页>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;
}
边栏推荐
- Numpy --- basic learning notes
- The differences between exit, exit (0), exit (1), exit ('0 '), exit ('1'), die and return in PHP
- markdown公式编辑教程
- Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition
- 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"?
- Xcode Revoke certificate
- Unity的三种单例模式(饿汉,懒汉,MonoBehaviour)
- Shader_ Animation sequence frame
- PHP实现执行定时任务的几种思路详解
- Is it reliable to open an account on Tongda letter with your mobile phone? Is there any potential safety hazard in such stock speculation
猜你喜欢

MySQL数据库基本操作-DQL-基本查询

Continuous creation depends on it!

Step by step monitoring platform ZABBIX

分步式监控平台zabbix

Dotween -- ease function

What are compiled languages and interpreted languages?

Mysql database basic operation DQL basic query

Tragedy caused by deleting the console statement

AE learning 01: AE complete project summary

torch.numel作用
随机推荐
leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
Laravel 中config的用法
The differences between exit, exit (0), exit (1), exit ('0 '), exit ('1'), die and return in PHP
Unity3d click events added to 3D objects in the scene
Leetcode-231-2的幂
php 自带过滤和转义函数
Bidding announcement: Panjin people's Hospital Panjin hospital database maintenance project
Leetcode-136- number that appears only once (solve with XOR)
Enterprise log analysis system elk
You Yuxi, coming!
分步式监控平台zabbix
How does laravel run composer dump autoload without emptying the classmap mapping relationship?
asyncio 概念和用法
[excelexport], Excel to Lua, JSON, XML development tool
Shader_ Animation sequence frame
山东老博会,2022中国智慧养老展会,智能化养老、适老科技展
Eye of depth (VII) -- Elementary Transformation of matrix (attachment: explanation of some mathematical models)
删除 console 语句引发的惨案
JS 模块化
Laravel 服务提供者实例教程 —— 创建 Service Provider 测试实例