当前位置:网站首页>LeetCode_21(合并两个有序链表)
LeetCode_21(合并两个有序链表)
2022-06-10 14:16:00 【***】
题目描述: 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例 1:
输入:l1 = [1,2,4], l2 = [1,3,4]
输出:[1,1,2,3,4,4]
示例 2:
输入:l1 = [], l2 = []
输出:[]
示例 3:
输入:l1 = [], l2 = [0]
输出:[0]提示:
两个链表的节点数目范围是 [0, 50]
-100 <= Node.val <= 100
l1 和 l2 均按 非递减顺序 排列
//递归
/**
* 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;
if(list2==null)return list1;
//将两个中小的那一个放入新链表,另外那一个称为大的那一个
ListNode list3=list1.val<list2.val?list1:list2;
//将小的后面的那一个与刚刚大的那一个相比较
list3.next=mergeTwoLists(list3.next,list1.val>=list2.val?list1:list2);
return list3;
}
}
边栏推荐
- Redis基本使用1
- CentOS Linux 已死!Oracle Linux 可能是它的更好替代品
- 微信小程序 日期比较,计算天数
- Simulated 100 questions and online simulated examination for safety production management personnel of hazardous chemical production units in 2022
- Leetcode 2293. 极大极小游戏(可以.一次过)
- The interview at the big factory was held at 10:00 a.m. and came out at 10:09 a.m. the question was really too
- 【重庆大学】初试复试资料分享(附考研群)
- 2022广东省安全员A证第三批(主要负责人)考试练习题及在线模拟考试
- 22.6.7成功使用doc2vec模型生成嵌入向量
- 短文本重复率快速检测
猜你喜欢

【大咖秀】博睿数据眼中的AIOps,选择正确的赛道正确的人
![[discrete mathematics review series] v. some special charts](/img/4d/a52ba801b0421245d5e3828defc96d.png)
[discrete mathematics review series] v. some special charts

C multithreading learning note 3

22.6.7成功使用doc2vec模型生成嵌入向量
![[discrete mathematics review series] VI. tree](/img/ec/2755c9328a04483080789b366f2d96.png)
[discrete mathematics review series] VI. tree
![[notes] notes on C language array pointer, structure + two-dimensional array pointer](/img/77/5923754b12ccfe0bcba7e46617de86.png)
[notes] notes on C language array pointer, structure + two-dimensional array pointer

2022 examination question bank and online simulation examination for main principals of hazardous chemical business units

【离散数学期复习系列】五、一些特殊的图
![[C language] pointer function, function pointer and array function](/img/3c/e8816c4fe52c1757f4f9e17d19c285.png)
[C language] pointer function, function pointer and array function

Mutual transformation among lists, arrays and tensors
随机推荐
C multithreading learning note 3
二叉树和图2
[discrete mathematics review series] v. some special charts
anaconda安装opencv(cv2),在jupyter notebook中使用
Main features of IIC bus / communication process / read / write process
Kotlin bubbling algorithm, Gaud map filters out the data whose distance between two points is less than 50, and does not repeat the display
markdown设置字体为红色
erp odoo 权限管理5年系统设置经验小结 真经验分享
IIC总线的主要特点/通信过程/读写过程
Flutter listview, column, row learning personal summary 2
Flutter 页面跳转 传参,TabBar学习总结5
CentOS Linux 已死!Oracle Linux 可能是它的更好替代品
数仓的基本概念
Flutter Icon Stack LIsttitle...学习总结3
【離散數學期複習系列】二、一階邏輯(謂詞邏輯)
2022年危险化学品生产单位安全生产管理人员考试模拟100题及在线模拟考试
P3379 [template] nearest common ancestor (LCA)
jdbc对数据库增删改查
UE5如何将屏幕坐标转为世界坐标和世界方向
c#浅拷贝与深拷贝自定义的类的List<>
