当前位置:网站首页>Leetcode: merge two ordered linked lists_ twenty-one
Leetcode: merge two ordered linked lists_ twenty-one
2022-06-28 21:05:00 【Yuyy】
Ideas
Basic shift double pointer , And the idea of merging
subject
Merge two ascending linked lists into a new Ascending Link list and return . The new linked list is made up of all the nodes of the given two linked lists .
Example 1:
Input :l1 = [1,2,4], l2 = [1,3,4]
Output :[1,1,2,3,4,4]Example 2:
Input :l1 = [], l2 = []
Output :[]Example 3:
Input :l1 = [], l2 = [0]
Output :[0]Tips :
- The range of the number of nodes in the two linked lists is
[0, 50] -100 <= Node.val <= 100l1andl2All according to Non decreasing order array
Related Topics
- recursive
- Linked list
- 1887
- 0
Code
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
// Virtual head nodes are very useful
ListNode dummy = new ListNode();
// Pointer of the result linked list
ListNode p = dummy;
// Double pointer
ListNode p1 = l1;
ListNode p2 = l2;
while (p1 != null && p2 != null) {
if (p1.val < p2.val) {
p.next = p1;
// Double pointer movement
p1 = p1.next;
} else {
p.next = p2;
p2 = p2.next;
}
// Don't forget to move the result pointer
p = p.next;
}
// Connect the long section directly to the result list
if (p1 != null) {
p.next = p1;
} else {
p.next = p2;
}
return dummy.next;
}Post Views: 148
边栏推荐
- LeetCode1114. 按序打印
- The blocks problem (uva101) Purple Book p110vector application
- Leetcode 36. 有效的数独(可以,一次过)
- ANR问题--相机相关的debug
- API gateway Apache APIs IX helps the evolution of snowball dual active architecture
- [Note: analog MOS integrated circuit] bandgap reference (basic principle + current mode + voltage mode circuit explanation)
- mysql-发生系统错误1067
- [Note: circuit intégré MOS analogique] référence de bande Gap (principe de base + mode courant + circuit en mode tension)
- 图神经网络也能用作CV骨干模型,华为诺亚ViG架构媲美CNN、Transformer
- 券商公司开户哪个最靠谱最安全呢
猜你喜欢

我也差点“跑路”

Pie (poj3122) super detailed and easy to understand binary introduction
How to recover after Oracle delete accidentally deletes table data

Data standardization processing

APISIX 助力中东社交软件,实现本地化部署

Leetcode daily question - 515 Find the maximum value in each tree row

【筆記:模擬MOS集成電路】帶隙基准(基本原理+電流模+電壓模電路詳解)
![[Note: circuit intégré MOS analogique] référence de bande Gap (principe de base + mode courant + circuit en mode tension)](/img/cd/be62272d465ca990456c222b38df67.png)
[Note: circuit intégré MOS analogique] référence de bande Gap (principe de base + mode courant + circuit en mode tension)

接口测试流程

Apisik helps Middle East social software realize localized deployment
随机推荐
MySQL system error occurred 1067
LeetCode每日一题——324. 摆动排序 II
List of domestic database directory
Can you make money by speculating in stocks? It's safe to open an account
Alibaba cloud MSE full link grayscale solution practice based on Apache apisik
力扣树的进一步应用
1. integrate servlets
How to add logs to debug anr problems
【Try to Hack】Cobalt Strike(一)
ANR问题--相机相关的debug
在哪个软件上开户比较安全,开户流程是什么?
【笔记:模拟MOS集成电路】带隙基准(基本原理+电流模+电压模电路详解)
Bitbucket failed to pull the warehouse Using SSH
Analysis of variance
Application of the purple book p113map of ananagrams (uva156)
rapid ssl通配符证书八百一年是正版吗
API gateway Apache APIs IX helps the evolution of snowball dual active architecture
[book club issue 13] packaging format of video files
Keyword long
LeetCode116. 填充每个节点的下一个右侧节点指针