当前位置:网站首页>LeetCode每日一练 —— 21. 合并两个有序链表
LeetCode每日一练 —— 21. 合并两个有序链表
2022-07-27 18:33:00 【飞向星的客机】
前言
Wassup guys!我是Edison
今天是 LeetCode 上的 leetcode 21. 合并两个有序链表
Let’s get it!

1. 题目分析
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例 1:
示例 2:
示例 3:
2. 题目图解
这道题其实就是 归并 思路,每次取小的节点 尾插 到新链表
思路一
定义指针 list 1 指向第一个链表的 头节点,定义指针 list 2 指向第二个链表的 头节点;
再定义一个新链表,此时头节点 head 和 尾节点 tail 都为 NULL,如图所示
我们分析一下 尾插 的过程:
第 1 次,当 list 1 等于 list 2 时,就把 list 1 插入到新链表中,给 head 和 tail;
第 2 次,当 list 2 小于 list 1 时,就把 list 2 插入到新链表中,更新 tail;
第 3 次,当 list 1 小于 list 2 时,就把 list 1 插入到新链表中,更新 tail;
这里直接看动图演示
注意:
当其中任意一个链表指向 NULL 时,把剩余的链表的元素直接 拷贝 到新链表中。
比如,list 1 先指向 NULL,那么直接把 list 2 剩余的元素拿到 新链表 中,也不用再更新 tail 了,直接返回新链表的 头节点。
为什么不更新 tail 呢?
我们要返回的是 头节点 ,又不用返回新的 尾节点,这里定义的 tail 只是方便进行 尾插。
接口代码
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
if (list1 == NULL) {
return list2;
}
if (list2 == NULL) {
return list1;
}
struct ListNode* head = NULL, *tail = NULL;
while (list1 && list2) {
// list1 小于 list2
if ((list1->val) < (list2->val)) {
if (tail == NULL) {
head = tail = list1;
}
else {
tail->next = list1;
tail = list1;
// tail = tail->next
}
list1 = list1->next;
}
// list1 大于等于 list2
else {
if (tail == NULL) {
head = tail = list2;
}
else {
tail->next = list2;
tail = list2;
// tail = tail->next
}
list2 = list2->next;
}
}
if (list1) {
tail->next = list1;
}
if (list2) {
tail->next = list2;
}
return head;
}
提交结果
思路一
我们可以新建一个 带头节点 的新链表,也就是 哨兵位 的头节点,这个节点不存储有效数据。
那么这个 带头 和 不带头 的区别在哪里呢?
在 思路一 中,我们定义的新链表是不带头的,head 和 tail 都指向 NULL,所以在插入时,要进行判断,tail 是否为 NULL 的情况。
那么如果我设置一个 哨兵位 的头节点,那么根本不需要判断,哪怕此时一个值都没有,head 和 tail 都不可能为 空,所以在进行 尾插 时,直接把元素插入到 哨兵位 后面即可。
原理还是和 思路一 是一样的,所以直接看动图
注意:
返回的时候,不能返回 head,而是返回 head 指向的 next,也就是 哨兵位 的下一个节点;
因为题目给的两个链表是不带哨兵位的,所以我们合并以后,返回的也是不带哨兵位的
接口代码
// 带头节点的单链表
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
struct ListNode* head = NULL, *tail = NULL;
// 设置一个哨兵位
head = tail = (struct ListNode*)malloc(sizeof(struct ListNode));
head->next = NULL;
while (list1 && list2) {
if ((list1->val) < (list2->val)) {
tail->next = list1;
tail = list1;
list1 = list1->next;
}
else {
tail->next = list2;
tail = list2;
list2 = list2->next;
}
}
if (list1) {
tail->next = list1;
}
if (list2) {
tail->next = list2;
}
// 释放
struct ListNode* list = head->next;
free(head);
return list;
}
提交结果
边栏推荐
- 82.(cesium篇)cesium点在3d模型上运动
- 原生对象、内置对象、宿主对象的区别
- Academic sharing | Tsinghua University, Kang Chongqing: power system carbon measurement technology and application (matlab code implementation)
- Uncaught SyntaxError: redeclaration of let page
- Programmer growth Chapter 18: project launch
- 品牌列表案例
- Qt opengl 让物体在关照下动起来,形成动画
- Using dataX to realize efficient synchronization of MySQL data
- 推荐一款强大的搜索工具Listary
- 程序放在哪儿?
猜你喜欢

一文了解Pycharm快捷键
![[dart] a programming language for cross end development](/img/e1/1167a322bb9f276f2e00fb12414d17.png)
[dart] a programming language for cross end development

API Gateway介绍

Uncaught SyntaxError: redeclaration of let page

用户登录切换案例

sscanf 导致地址越界

One article to understand pychar shortcut key

程序放在哪儿?

The variable "lattice" or class "lattice.latticeeasy" (matlab) is not defined

Academic sharing | Tsinghua University, Kang Chongqing: power system carbon measurement technology and application (matlab code implementation)
随机推荐
Qt 链接MSSQL
走马灯案例
认识传输介质网络通信的介质
How does the industrial switch enter the web management interface?
SLIM:自监督点云场景流与运动估计(ICCV 2021)
opencv实现图片裁剪和缩放
推荐一款强大的搜索工具Listary
R语言使用epiDisplay包的lroc函数可视化logistic回归模型的ROC曲线并输出诊断表(diagnostic table)、可视化多条ROC曲线、使用legend函数为可视化图像添加图例
Know the transmission medium, the medium of network communication
Rk3399 platform development series explanation (process part) 15.36, understanding process and collaboration process
Uncaught SyntaxError: redeclaration of let page
NATAPP内网穿透工具外网访问个人项目
IPv4/IPv6、DHCP、网关、路由
Kingbasees heterogeneous database migration guide (4. Application migration process)
LeetCode每日一练 —— CM11 链表分割
ELK太重?试试KFC日志采集
Typroa 拼写检查: 缺少对于 中文 的字典文件
基于文件上传漏洞获得网站 shell 权限
Beijing / Shanghai / Guangzhou / Shenzhen dama-cdga/cdgp data governance certification registration conditions
R语言使用lm函数构建多元回归模型(Multiple Linear Regression)、并根据模型系数写出回归方程、使用deviance函数计算出模型的残差平方和