当前位置:网站首页>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;
}
边栏推荐
- Balanced binary tree (AVL)
- Wireless sensor networks -- ZigBee and 6LoWPAN
- Sysom case analysis: where is the missing memory| Dragon lizard Technology
- 47_Opencv中的轮廓查找 cv::findContours()
- 深度之眼(七)——矩阵的初等变换(附:数模一些模型的解释)
- Laravel 中config的用法
- Aerospace Hongtu information won the bid for the database system research and development project of a unit in Urumqi
- Markdown formula editing tutorial
- Vs tool word highlight with margin
- You Yuxi, coming!
猜你喜欢

The unity vector rotates at a point

Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation

Step by step monitoring platform ZABBIX

Xcode Revoke certificate

Numpy -- data cleaning

SysOM 案例解析:消失的内存都去哪了 !| 龙蜥技术

【Vulnhub靶场】THALES:1

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

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

Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!
随机推荐
Apache Doris just "graduated": why should we pay attention to this kind of SQL data warehouse?
PHP中exit,exit(0),exit(1),exit(‘0’),exit(‘1’),die,return的区别
Laravel5.1 路由 -路由分组
SysOM 案例解析:消失的内存都去哪了 !| 龙蜥技术
leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
[excelexport], Excel to Lua, JSON, XML development tool
hellogolang
Power of leetcode-231-2
01tire+链式前向星+dfs+贪心练习题.1
Particle effect for ugui
What about the pointer in neural network C language
Talk about the cloud deployment of local projects created by SAP IRPA studio
php 自带过滤和转义函数
Continuous creation depends on it!
Leetcode-231-2的幂
航天宏图信息中标乌鲁木齐某单位数据库系统研发项目
PHP realizes wechat applet face recognition and face brushing login function
Multiplication in pytorch: mul (), multiply (), matmul (), mm (), MV (), dot ()
2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo
SPI master rx time out中断