当前位置:网站首页>【21. 合并两个有序链表】
【21. 合并两个有序链表】
2022-06-22 19:41:00 【爱吃榴莲的喵星人】
一、题目描述
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。


二、提供方便走读代码的图

三、题目代码
写法一:
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
struct ListNode* head=NULL;
struct ListNode* tail=NULL;
if(list1==NULL)
return list2;
if(list2==NULL)
return list1;
while(list1&&list2)
{
if(list1->val < list2->val)
{
if(head==NULL)
{
head=tail=list1;
}
else
{
tail->next=list1;
tail=tail->next;
}
list1=list1->next;
}
else
{
if(head==NULL)
{
head=tail=list2;
}
else
{
tail->next=list2;
tail=tail->next;
}
list2=list2->next;
}
}
if(list1!=NULL)
{
tail->next=list1;
}
if(list2!=NULL)
{
tail->next=list2;
}
return head;
}
写法二:带头结点
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
struct ListNode* head=(struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* tail=head;
if(list1==NULL)
return list2;
if(list2==NULL)
return list1;
while(list1&&list2)
{
if(list1->val < list2->val)
{
tail->next=list1;
tail=tail->next;
list1=list1->next;
}
else
{
tail->next=list2;
tail=tail->next;
list2=list2->next;
}
}
if(list1!=NULL)
{
tail->next=list1;
}
if(list2!=NULL)
{
tail->next=list2;
}
struct ListNode* list=head->next;
free(head);
return list;
}
以上是本篇文章的全部内容,如果文章有错误或者有看不懂的地方,多和喵博主交流。互相学习互相进步。如果这篇文章对你有帮助,可以给喵博主一个关注,你们的支持是我最大的动力。
边栏推荐
- 密码学系列之:PKI的证书格式表示X.509
- Visualization of R language nutrient dataset
- Container container runtime (2): which is better for you, yum installation or binary installation?
- Objective-C不同数据类型占用字节大小
- Resolved: can there be multiple auto incrementing columns in a table
- 启牛送的券商账户是安全的吗?启牛提供的券商账户是真的?
- 访问成功但抛出异常:Could not find acceptable representation
- 【奇葩需求之记录对象不同的日志】
- 88-被广为流传的参数优化, 是蜜糖还是毒药?
- The real king of cache
猜你喜欢

采用网络远程访问树莓派。

访问成功但抛出异常:Could not find acceptable representation
MySQL中如何计算同比和环比

MySQL Basics - functions

SwiftUI如何模拟视图发光增大的动画效果

深度学习常用损失函数总览:基本形式、原理、特点
![[proteus simulation] 74LS138 decoder water lamp](/img/30/7dbdead9c18788cd946b5541e76443.png)
[proteus simulation] 74LS138 decoder water lamp

超快变形金刚 | 用Res2Net思想和动态kernel-size再设计 ViT,超越MobileViT

慕课5、服务发现-Nacos

Introduction of neural network (BP) in Intelligent Computing
随机推荐
CVPR 2022 Oral | 视频文本预训练新SOTA,港大、腾讯ARC Lab推出基于多项选择题的借口任务
How to calculate yoy and mom in MySQL
理财产品在双休日可以赎回吗?
百家讲坛 雍正十三年(下部)
一行代码为特定状态绑定SwiftUI视图动画
密码学系列之:PKI的证书格式表示X.509
SwiftUI如何模拟视图发光增大的动画效果
R language Midwest dataset visualization
[observation] innovation in the software industry has entered a "new cycle". How can we make a new start in the changing situation?
启牛送的券商账户是安全的吗?启牛提供的券商账户是真的?
2022团体程序设计天梯赛L1
[resolved] -go_ out: protoc-gen-go: Plugin failed with status code 1.
71-对2010年阿里一道Oracle DBA面试题目的分析
Introduction of neural network (BP) in Intelligent Computing
Raspberry pie environment settings
Stochastic Adaptive Dynamics of a Simple Market as a Non-Stationary Multi-Armed Bandit Problem
Solutions to Oracle system/ user locking
Lora technology -- Lora signal changes from data to Lora spread spectrum signal, and then from RF signal to data through demodulation
73-找到业务高峰时段的sql示例(报表开发类)
Cryptography series: certificate format representation of PKI X.509