当前位置:网站首页>leetcode 剑指 Offer 25. 合并两个排序的链表
leetcode 剑指 Offer 25. 合并两个排序的链表
2022-07-30 08:52:00 【kt1776133839】
题目描述:
输入两个递增排序的链表,合并这两个链表并使新链表中的节点仍然是递增排序的。
样例:
示例1:
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4
限制:
0 <= 链表长度 <= 1000
解题思路:
根据题目描述, 链表 l1l_1l1 , l2l_2l2 是 递增 的,因此容易想到使用双指针 l1l_1l1 和 l2l_2l2 遍历两链表,根据 l1.vall_1.vall1.val 和 l2.vall_2.vall2.val 的大小关系确定节点添加顺序,两节点指针交替前进,直至遍历完毕。
引入伪头节点: 由于初始状态合并链表中无节点,因此循环第一轮时无法将节点添加到合并链表中。解决方案:初始化一个辅助节点 dumdumdum 作为合并链表的伪头节点,将各节点添加至 dumdumdum 之后。

算法流程:
初始化: 伪头节点 dumdumdum ,节点 curcurcur 指向 dumdumdum 。
循环合并: 当 l1l_1l1 或 l2l_2l2 为空时跳出;
当 l1.val<l2.vall_1.val < l_2.vall1.val<l2.val 时: curcurcur 的后继节点指定为 l1l_1l1 ,并 l1l_1l1 向前走一步;
当 l1.val≥l2.vall_1.val \geq l_2.vall1.val≥l2.val 时: curcurcur 的后继节点指定为 l2l_2l2 ,并 l2l_2l2 向前走一步 ;
节点 curcurcur 向前走一步,即 cur=cur.nextcur = cur.nextcur=cur.next 。
合并剩余尾部: 跳出时有两种情况,即 l1l_1l1 为空 或 l2l_2l2 为空。
若 l1≠nulll_1 \ne nulll1=null : 将 l1l_1l1 添加至节点 curcurcur 之后;
否则: 将 l2l_2l2 添加至节点 curcurcur 之后。
返回值: 合并链表在伪头节点 dumdumdum 之后,因此返回 dum.nextdum.nextdum.next 即可。
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dum = new ListNode(0), cur = dum;
while(l1 != null && l2 != null) {
if(l1.val < l2.val) {
cur.next = l1;
l1 = l1.next;
}
else {
cur.next = l2;
l2 = l2.next;
}
cur = cur.next;
}
cur.next = l1 != null ? l1 : l2;
return dum.next;
}
}
边栏推荐
- 都说FPGA高端,它到底能干啥?
- 【愚公系列】2022年07月 Go教学课程 021-Go容器之切片操作
- Integral Special Notes-Three Formulas for Curve Area Integral
- 2022杭电多校第二场
- Apache DolphinScheduler's new generation of distributed workflow task scheduling platform in practice - Part 1
- 【 HMS core 】 【 】 the FAQ HMS Toolkit collection of typical questions 1
- 硬件工程师
- 2022/07/29 学习笔记 (day19)异常处理
- leetcode 剑指 Offer 42. 连续子数组的最大和
- [Fun BLDC series with zero basics] Taking GD32F30x as an example, the timer related functions are explained in detail
猜你喜欢

Kotlin 值类 - value class

【云原生】Kubernetes入门详细讲解

Integral Topic Notes - Path Independent Conditions
![[Yugong Series] July 2022 Go Teaching Course 021-Slicing Operation of Go Containers](/img/ff/f3de4952d64afe36c515a2220bfe9d.png)
[Yugong Series] July 2022 Go Teaching Course 021-Slicing Operation of Go Containers

The FPGA based protocol 2: the I2C read and write E squared PROM

百度paddleocr检测训练

【愚公系列】2022年07月 Go教学课程 021-Go容器之切片操作

Concise Notes on Integrals - Types of Curve Integrals of the Second Kind

Excel xlsx file not supported两种解决办法【杭州多测师】【杭州多测师_王sir】

聊聊 MySQL 事务二阶段提交
随机推荐
Unreal Engine Graphic Notes: could not be compiled. Try rebuilding from source manually. Problem solving
Oracle 创建和操作表
conda 导出/导出配置好的虚拟环境
电源完整性的去耦和层间耦合电容
知识图谱之Cypher语言的使用
Unable to locate the program input point ucrtbase.abort on the dynamic link library api-ms-win-crt-runtime-|1-1-0.dll
【愚公系列】2022年07月 Go教学课程 021-Go容器之切片操作
积分专题笔记-积分的定义
Detailed description of iperf3 parameter options
PyQt5快速开发与实战 7.4 事件处理机制入门 and 7.5 窗口数据传递
The sword refers to offer 48: the longest non-repeating substring
MySQL Explain usage and parameter detailed explanation
STM8L_库函数-模板搭建
iperf3 参数选项详细说明
Taosi TDengine 2.6+ optimization parameters
一个低级错误导致的诡异现象——走近科学能拍三集,(C语言)最简单的数组元素读取,不正确!?
Apache DolphinScheduler's new generation of distributed workflow task scheduling platform in practice - Part 1
硬件工程师
微软 SQL 服务器被黑,带宽遭到破坏
Activating data potential Amazon cloud technology reshapes cloud storage "family bucket"