当前位置:网站首页>[record of question brushing] 21. Merge two ordered linked lists
[record of question brushing] 21. Merge two ordered linked lists
2022-07-25 19:12:00 【InfoQ】
One 、 Title Description

Input :l1 = [1,2,4], l2 = [1,3,4]
Output :[1,1,2,3,4,4]
Input :l1 = [], l2 = []
Output :[]
Input :l1 = [], l2 = [0]
Output :[0]
- The range of the number of nodes in the two linked lists is [0, 50]
- -100 <= Node.val <= 100
- l1 and l2 All according to Non decreasing order array
II. Train of thought analysis
Double pointer solution because l1 and l2 All according to Non decreasing order array 3、 ... and 、 Code implementation
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(-1);
ListNode prev = dummy;
while (l1 != null && l2 != null) {
if (l1.val <= l2.val) {
prev.next = l1;
l1 = l1.next;
} else {
prev.next = l2;
l2 = l2.next;
}
prev = prev.next;
}
// If there is a linked list that has not been traversed
prev.next = l1 == null ? l2 : l1;
return dummy.next;
}
}
Complexity analysis
- Time complexity : Scan two linked lists . The complexity is
m,nFor the length of two linked lists
- Spatial complexity :
Running results

summary
边栏推荐
- Software testing (mind mapping)
- Clip can also do segmentation tasks? The University of Gottingen proposed a model clipseg that uses text and image prompt and can do three segmentation tasks at the same time, squeezing out the clip a
- 小程序毕设作品之微信校园维修报修小程序毕业设计成品(4)开题报告
- Pymoo学习 (8):Gradients
- Pymoo learning (8):grades
- [web technology] 1391 page visualization building tool, previous life and present life
- Alibaba cloud technology expert haochendong: cloud observability - problem discovery and positioning practice
- Youwei low code: use resolutions
- FPGA based 1080p 60Hz bt1120 interface debugging process record
- How to prohibit the use of 360 browser (how to disable the built-in browser)
猜你喜欢

Baklib: make excellent product instruction manual

Full scale and Xuan of C key
![[encryption weekly] has the encryption market recovered? The cold winter has not thawed yet! Check the major events in the encryption market last week!](/img/6d/b037208996ce52016d014062deaa1f.jpg)
[encryption weekly] has the encryption market recovered? The cold winter has not thawed yet! Check the major events in the encryption market last week!

Single arm routing experiment demonstration (Huawei router device configuration)

Talk about 15 tips of SQL optimization

Fruit chain "siege": it's a journey of sweetness and bitterness next to apples

CLIP还能做分割任务?哥廷根大学提出一个使用文本和图像prompt,能同时作三个分割任务的模型CLIPSeg,榨干CLIP能力...
![[Detr for 3D object detection] 3detr: an end to end transformer model for 3D object detection](/img/22/426bcb8641db5bfe07e8aacf5e8427.png)
[Detr for 3D object detection] 3detr: an end to end transformer model for 3D object detection

这种动态规划你见过吗——状态机动态规划之股票问题(上)

QIIME2得到PICRUSt2结果后如何分析
随机推荐
Pymoo learning (7): Parallelization
Baklib: make excellent product instruction manual
With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
Korean AI team plagiarizes shock academia! One tutor with 51 students, or plagiarism recidivist
Cross Site Request Forgery in PHP
【小程序开发】你了解小程序开发吗?
HTTP cache tongtianpian, there may be something you want
小程序毕设作品之微信校园维修报修小程序毕业设计成品(2)小程序功能
Alibaba cloud technology expert haochendong: cloud observability - problem discovery and positioning practice
Basic music theory -- configuring chords
What is the application value of MES management system
【云原生之kubernetes】kubernetes集群下Secret存储对象的管理
网上商城系统MySql数据库设计项目实战
How to change the chords after the tune of the song is changed
【小程序开发】宿主环境详解
无惧高温暴雨,有孚网络如何保您无忧?
App test point (mind map)
小程序毕设作品之微信校园维修报修小程序毕业设计成品(8)毕业设计论文模板
QT compiled successfully, but the program could not run
Pymoo学习 (7):并行化Parallelization