当前位置:网站首页>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
边栏推荐
- Please allow the "imperfection" of the current domestic Tob
- 稳定性总结
- How to recover after Oracle delete accidentally deletes table data
- Various types of long
- Stability summary
- iterator中的next()为什么要强转?
- Visualization of neural network structure in different frames
- Relevant calculation of sphere, etc
- Anr no response introduction
- 数据资产为王,如何解析企业数字化转型与数据资产管理的关系?
猜你喜欢

【Try to Hack】Cobalt Strike(一)

Application practice | 1billion data second level correlation. Huolala's OLAP System Evolution Based on Apache Doris (with PPT download)

Pyechart drawing multiple Y-axis line graphs

Analysis of variance

CNN-LSTM的flatten

【筆記:模擬MOS集成電路】帶隙基准(基本原理+電流模+電壓模電路詳解)

图神经网络也能用作CV骨干模型,华为诺亚ViG架构媲美CNN、Transformer

Racher add / delete node

Query rewriting for opengauss kernel analysis

postman简介与安装步骤
随机推荐
Leetcode daily question - 522 Longest special sequence II
Leetcode daily question - Sword finger offer II 091 Paint the house
Bitbucket 使用 SSH 拉取仓库失败的问题
Racher add / delete node
GlobalSign的泛域名SSL证书
With a market value of $120billion, how did intuit, an old tax giant, do it?
Win 10 create a gin framework project
不同框架的绘制神经网络结构可视化
API gateway Apache APIs IX helps the evolution of snowball dual active architecture
How to open an account in great wisdom? Is it safe
二叉树类题目 力扣
国产数据库名录一览
How to "calculate" in the age of computing power? The first mover advantage of "convergence of computing and networking" is very important!
Leetcode daily question - 324 Swing sort II
Leetcode daily question - 30 Concatenate substrings of all words
The comprehensive application of the setstack computer (uva12096) Purple Book p116stl
with torch.no_grad():的使用原因
MySQL system error occurred 1067
开通挖财账号安全吗?是靠谱的吗?
resilience4j 重试源码分析以及重试指标采集