当前位置:网站首页>【Hot100】21. 合并两个有序链表
【Hot100】21. 合并两个有序链表
2022-07-02 18:54:00 【王六六的IT日常】
21. 合并两个有序链表
简单题
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
递归
发现有意思的一个图哈哈哈哈哈
- 终止条件:当两个链表都为空时
- 如何递归:我们判断list1 和 list2 的头结点哪个更小,然后较小结点的 next 指针指向其余结点的合并结果。(调用递归)
- 如果 list1 的 val 值更小,则将 list1 .next 与排序好的链表头相接,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; } * } */
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;
}
}
}
- 迭代
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;
}
边栏推荐
- AcWing 1131. Saving Private Ryan (the shortest way)
- 思考变量引起的巨大变化
- Kt148a voice chip IC software reference code c language, first-line serial port
- Data Lake (XII): integration of spark3.1.2 and iceberg0.12.1
- checklistbox控件用法总结
- Idea editor removes SQL statement background color SQL statement warning no data sources are configured to run this SQL And SQL dialect is not config
- Self-Improvement! Daliangshan boys all award Zhibo! Thank you for your paper
- What are the benefits of multi terminal applet development? Covering Baidu applet, Tiktok applet, wechat applet development, and seizing the multi platform traffic dividend
- Detailed tutorial on installing stand-alone redis
- 职场四象限法则:时间管理四象限与职场沟通四象限「建议收藏」
猜你喜欢
Zabbix5 client installation and configuration
c语言链表--待补充
数据湖(十二):Spark3.1.2与Iceberg0.12.1整合
Istio1.12:安装和快速入门
Introduction to program ape (XII) -- data storage
勵志!大凉山小夥全獎直博!論文致謝看哭網友
Motivation! Big Liangshan boy a remporté le prix Zhibo! Un article de remerciement pour les internautes qui pleurent
mysql函数
Implementation of online shopping mall system based on SSM
SQLite 3.39.0 发布,支持右外连接和全外连接
随机推荐
Istio deployment: quickly start microservices,
Introduction to mongodb chapter 03 basic concepts of mongodb
[译]深入了解现代web浏览器(一)
Istio部署:快速上手微服务,
AcWing 1131. 拯救大兵瑞恩 题解(最短路)
MySQL
Think about the huge changes caused by variables
AcWing 1127. 香甜的黄油 题解(最短路—spfa)
Overview of browser caching mechanism
upload-labs
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?
Data Lake (XII): integration of spark3.1.2 and iceberg0.12.1
R language uses econcharts package to create microeconomic or macroeconomic maps, and indifference function to visualize indifference curve
自動生成VGG圖像注釋文件
高并发下如何避免产生重复数据?
Chapter 7 - class foundation
VBScript详解(一)
良心总结!Jupyter Notebook 从小白到高手,保姆教程来了!
Is there any security guarantee for the ranking of stock and securities companies
AcWing 1126. 最小花费 题解(最短路—dijkstra)