当前位置:网站首页>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
边栏推荐
猜你喜欢

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

Analysis of variance

Pie (poj3122) super detailed and easy to understand binary introduction

题解 Pie(POJ3122)超详细易懂的二分入门

基于 Apache APISIX 的自动化运维平台
![[learning notes] Introduction to principal component analysis](/img/24/a760d1cd095a967ef258b623eb465c.png)
[learning notes] Introduction to principal component analysis

【Try to Hack】Cobalt Strike(一)

Bitbucket failed to pull the warehouse Using SSH

ref属性,props配置,mixin混入,插件,scoped样式

Figure neural network can also be used as CV backbone model. Huawei Noah Vig architecture is comparable to CNN and transformer
随机推荐
Bitbucket failed to pull the warehouse Using SSH
I almost ran away
APISIX 助力中东社交软件,实现本地化部署
LeetCode:合并两个有序链表_21
How to open an account in great wisdom? Is it safe
LeetCode188. The best time to buy and sell stocks IV
Pyechart drawing multiple Y-axis line graphs
LeetCode1114. 按序打印
Pie (poj3122) super detailed and easy to understand binary introduction
Apisik helps Middle East social software realize localized deployment
LeetCode:合并K个升序链表_23
学习太极创客 — MQTT 第二章(七)ESP8266 MQTT 遗嘱应用
Leetcode daily question - 515 Find the maximum value in each tree row
with torch. no_ Grad(): reason for using
关于不完全类型的认识
【学习笔记】主成分分析法介绍
Is the rapid SSL wildcard certificate genuine in 1981
视频号如何下载视频?来看超简单方法!
Bitbucket 使用 SSH 拉取仓库失败的问题
LeetCode121. 买卖股票的最佳时机