当前位置:网站首页>【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;
}
边栏推荐
- Think about the huge changes caused by variables
- [ERP software] what are the dangers of the secondary development of ERP system?
- Kt148a voice chip IC user end self replacement voice method, upper computer
- AcWing 1135. Happy New Year (shortest path + search)
- 分享几个图床网址,便于大家分享图片
- From 20s to 500ms, I used these three methods
- 外包干了三年,废了...
- 自动化制作视频
- 定了,就是它!
- 测试人员如何做不漏测?这7点就够了
猜你喜欢

Educational codeforces round 129 (rated for Div. 2) supplementary problem solution

Zabbix5 client installation and configuration

【NLP】一文详解生成式文本摘要经典论文Pointer-Generator

Motivation! Big Liangshan boy a remporté le prix Zhibo! Un article de remerciement pour les internautes qui pleurent

ShardingSphere-JDBC5.1.2版本关于SELECT LAST_INSERT_ID()本人发现还是存在路由问题

burp 安装 license key not recognized

励志!大凉山小伙全奖直博!论文致谢看哭网友

RPD product: super power squad nanny strategy

API documentation tool knife4j usage details

AcWing 340. Solution to communication line problem (binary + double ended queue BFS for the shortest circuit)
随机推荐
SQLite 3.39.0 发布,支持右外连接和全外连接
At compilation environment setup -win
Detailed tutorial on installing stand-alone redis
浏览器缓存机制概述
Motivation! Big Liangshan boy a remporté le prix Zhibo! Un article de remerciement pour les internautes qui pleurent
【Hot100】23. 合并K个升序链表
GCC: Graph Contrastive Coding for Graph Neural NetworkPre-Training
R语言使用econocharts包创建微观经济或宏观经济图、indifference函数可视化无差异曲线(indifference curve)
KT148A语音芯片ic的软件参考代码C语言,一线串口
Embedded (PLD) series, epf10k50rc240-3n programmable logic device
有时候只查询一行语句,执行也慢
CheckListBox control usage summary
MySQL function
嵌入式(PLD) 系列,EPF10K50RC240-3N 可编程逻辑器件
AcWing 1137. Select the best line solution (the shortest circuit)
【Hot100】22. 括号生成
JS如何取整数
Dictionaries
pytorch 模型保存的完整例子+pytorch 模型保存只保存可训练参数吗?是(+解决方案)
Postman接口测试实战,这5个问题你一定要知道