当前位置:网站首页>【Hot100】21. Merge two ordered linked lists
【Hot100】21. Merge two ordered linked lists
2022-07-02 19:57:00 【Wang Liuliu's it daily】
21. Merge two ordered lists
Simple questions
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 .
recursive
Find an interesting picture hahahaha
- Termination conditions : When both linked lists are empty
- How to recurse : We can judge list1 and list2 Which is smaller , Then the smaller nodes next The pointer points to the merge results of other nodes .( Call recursion )
- If list1 Of val Less valuable , Will list1 .next Connect with the sorted chain header ,list2 Empathy
- Return value : Each layer call returns the sorted chain header
/** * 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 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;
}
}
}
- iteration
private ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(0);
ListNode tail = dummyHead;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
tail.next = l1;
l1 = l1.next;
} else {
tail.next = l2;
l2 = l2.next;
}
tail = tail.next;
}
tail.next = l1 == null? l2: l1;
return dummyHead.next;
}
边栏推荐
- CS5268完美代替AG9321MCQ Typec多合一扩展坞方案
- AcWing 181. Turnaround game solution (search ida* search)
- AcWing 1137. Select the best line solution (the shortest circuit)
- 勵志!大凉山小夥全獎直博!論文致謝看哭網友
- Build a master-slave mode cluster redis
- AcWing 1127. Sweet butter solution (shortest path SPFA)
- Yes, that's it!
- Overview of browser caching mechanism
- burp 安装 license key not recognized
- 攻防世界pwn题:Recho
猜你喜欢
随机推荐
Understanding and function of polymorphism
One side is volume, the other side is layoff. There are a lot of layoffs in byte commercialization department. What do you think of this wave?
AcWing 343. Sorting problem solution (Floyd property realizes transitive closure)
JS如何取整数
AcWing 341. Optimal trade solution (shortest path, DP)
Introduction to mongodb chapter 03 basic concepts of mongodb
台湾SSS鑫创SSS1700替代Cmedia CM6533 24bit 96KHZ USB音频编解码芯片
Motivation! Big Liangshan boy a remporté le prix Zhibo! Un article de remerciement pour les internautes qui pleurent
AcWing 1135. Happy New Year (shortest path + search)
面试经验总结,为你的offer保驾护航,满满的知识点
VBScript详解(一)
有时候只查询一行语句,执行也慢
Google Earth engine (GEE) - Landsat 9 image full band image download (Beijing as an example)
Google Earth Engine(GEE)——Landsat 9影像全波段影像下载(北京市为例)
AcWing 340. Solution to communication line problem (binary + double ended queue BFS for the shortest circuit)
pytorch 模型保存的完整例子+pytorch 模型保存只保存可训练参数吗?是(+解决方案)
AcWing 1128. Messenger solution (shortest path Floyd)
【每日一题】241. 为运算表达式设计优先级
Burp install license key not recognized
良心总结!Jupyter Notebook 从小白到高手,保姆教程来了!