当前位置:网站首页>LeetCode:合并两个有序链表_21
LeetCode:合并两个有序链表_21
2022-06-28 20:59:00 【Yuyy】
思路
基础班双指针,还有归并的思想
题目
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例 1:
输入:l1 = [1,2,4], l2 = [1,3,4]
输出:[1,1,2,3,4,4]示例 2:
输入:l1 = [], l2 = []
输出:[]示例 3:
输入:l1 = [], l2 = [0]
输出:[0]提示:
- 两个链表的节点数目范围是
[0, 50] -100 <= Node.val <= 100l1和l2均按 非递减顺序 排列
Related Topics
- 递归
- 链表
- 1887
- 0
代码
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
// 虚拟头结点挺有用的
ListNode dummy = new ListNode();
// 结果链表的指针
ListNode p = dummy;
// 双指针
ListNode p1 = l1;
ListNode p2 = l2;
while (p1 != null && p2 != null) {
if (p1.val < p2.val) {
p.next = p1;
// 双指针移动
p1 = p1.next;
} else {
p.next = p2;
p2 = p2.next;
}
// 别忘了结果指针也要移动
p = p.next;
}
// 将长的那节直接和结果链表接上
if (p1 != null) {
p.next = p1;
} else {
p.next = p2;
}
return dummy.next;
}Post Views: 148
边栏推荐
- [learning notes] cluster analysis
- ref属性,props配置,mixin混入,插件,scoped样式
- 题解 The SetStack Computer(UVa12096)紫书P116STL的综合应用
- ID access card copied to mobile phone_ How to turn a mobile phone into an access card mobile NFC copy access card graphic tutorial
- Leetcode daily question - 30 Concatenate substrings of all words
- Characters and integers
- Is it safe for CICC fortune to open an account? Let's talk about CICC fortune
- How to analyze the relationship between enterprise digital transformation and data asset management?
- resilience4j 重试源码分析以及重试指标采集
- 开通挖财账号安全吗?是靠谱的吗?
猜你喜欢

数据资产为王,如何解析企业数字化转型与数据资产管理的关系?

ThreadLocal principle
![[learning notes] Introduction to principal component analysis](/img/24/a760d1cd095a967ef258b623eb465c.png)
[learning notes] Introduction to principal component analysis

力扣树的进一步应用

RT-Thread线程同步与线程通信

Bitbucket 使用 SSH 拉取仓库失败的问题

ref属性,props配置,mixin混入,插件,scoped样式
![[Note: analog MOS integrated circuit] bandgap reference (basic principle + current mode + voltage mode circuit explanation)](/img/cd/be62272d465ca990456c222b38df67.png)
[Note: analog MOS integrated circuit] bandgap reference (basic principle + current mode + voltage mode circuit explanation)

学习太极创客 — MQTT 第二章(八)ESP8266 MQTT 用户密码认证

RT thread thread synchronization and thread communication
随机推荐
稳定性总结
pyechart绘制多条y轴折线图
Fix the simulator that cannot be selected by flutter once
I almost ran away
Bitbucket 使用 SSH 拉取仓库失败的问题
[try to hack] cobalt strike (I)
The principle and source code analysis of Lucene index construction
输入和输出实型数据
Leetcode 36. Effective Sudoku (yes, once)
Is it safe to open a dig money account? Is it reliable?
Leetcode 36. 有效的数独(可以,一次过)
How to analyze the relationship between enterprise digital transformation and data asset management?
不同框架的绘制神经网络结构可视化
ref属性,props配置,mixin混入,插件,scoped样式
Stability summary
T检验(检验两个总体的均值差异是否显著)
【学习笔记】因子分析
How to analyze the relationship between enterprise digital transformation and data asset management?
Understanding of incomplete types
【筆記:模擬MOS集成電路】帶隙基准(基本原理+電流模+電壓模電路詳解)