当前位置:网站首页>【LeetCode】3. Merge two sorted lists · merge two ordered linked lists
【LeetCode】3. Merge two sorted lists · merge two ordered linked lists
2022-07-03 07:42:00 【AQin1012】
Title Description
Description in English
You are given the heads of two sorted linked lists list1 and list2. Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists. Return the head of the merged linked list.
English address
Description of Chinese version
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]
Constraints· Tips :
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
Address of Chinese version
Their thinking
My first reaction was to read all , Then compare one by one , Then put it into a new linked list , Then I thought about it , The linked lists of these two inputs are already in order , In fact, you can read data from two linked lists and compare them , The smaller one is stored in the new linked list and points to the next data of the linked list , Big continues to compare with this new data …… repeat , Until a linked list is empty , Add the remaining data of the non empty linked list to the end of the new linked list , Just return to the new linked list .
How to solve the problem
My version
Recursive method

public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if (list1 == null) {
return list2;
}
else if (list2 == null) {
return list1;
}
else if (list1.val <= list2.val) {
list1.next = mergeTwoLists(list1.next, list2);
return list1;
} else {
list2.next = mergeTwoLists(list1, list2.next);
return list2;
}
}
// 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;
}
}
Official edition
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode prehead = new ListNode(-1);
ListNode prev = prehead;
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;
}
// After the merger l1 and l2 At most, only one has not been merged yet , We can directly point the end of the linked list to the list that has not been merged
prev.next = l1 == null ? l2 : l1;
return prehead.next;
}
// 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;
}
}

Welcome students who have better methods to kick me in the comment area (。・ω・。)ノ
边栏推荐
- 【MySQL 13】安装MySQL后第一次修改密码,可以可跳过MySQL密码验证进行登录
- opensips与对方tls sip trunk对接注意事项
- PDO and SDO concepts
- Technical dry goods Shengsi mindspire lite1.5 feature release, bringing a new end-to-end AI experience
- Common operations of JSP
- 技术干货|昇思MindSpore Lite1.5 特性发布,带来全新端侧AI体验
- HDMI2.1与HDMI2.0的区别以及转换PD信号。
- 技术干货|利用昇思MindSpore复现ICCV2021 Best Paper Swin Transformer
- Go language foundation ----- 18 ----- collaboration security, mutex lock, read-write lock, anonymous lock, sync Once
- The concept of C language pointer
猜你喜欢

Technical dry goods | alphafold/ rosettafold open source reproduction (2) - alphafold process analysis and training Construction

技术干货|昇思MindSpore可变序列长度的动态Transformer已发布!

技术干货|昇思MindSpore NLP模型迁移之LUKE模型——阅读理解任务

Go language foundation ------ 12 ------ JSON

Technical dry goods | reproduce iccv2021 best paper swing transformer with Shengsi mindspire

HDMI2.1与HDMI2.0的区别以及转换PD信号。

Shengsi mindspire is upgraded again, the ultimate innovation of deep scientific computing

Partage de l'expérience du projet: mise en œuvre d'un pass optimisé pour la fusion IR de la couche mindstore

【开发笔记】基于机智云4G转接板GC211的设备上云APP控制

技术干货|AI框架动静态图统一的思考
随机推荐
Go language foundation ----- 19 ----- context usage principle, interface, derived context (the multiplexing of select can be better understood here)
哪一刻你才发现青春结束了
Hnsw introduction and some reference articles in lucene9
tp3.2和tp5.0的区别
項目經驗分享:實現一個昇思MindSpore 圖層 IR 融合優化 pass
Mail sending of vertx
昇思MindSpore再升级,深度科学计算的极致创新
Go language foundation ----- 01 ----- go language features
Go language foundation ----- 02 ----- basic data types and operators
VMware network mode - bridge, host only, NAT network
研究显示乳腺癌细胞更容易在患者睡觉时进入血液
Go language - loop statement
【MySQL 11】怎么解决MySQL 8.0.18 大小写敏感问题
PgSQL converts string to double type (to_number())
Partage de l'expérience du projet: mise en œuvre d'un pass optimisé pour la fusion IR de la couche mindstore
SQL create temporary table
Rabbit MQ message sending of vertx
Go language foundation ----- 07 ----- method
Analysis of the problems of the 12th Blue Bridge Cup single chip microcomputer provincial competition
PAT甲级 1029 Median
https://leetcode.com/problems/merge-two-sorted-lists/