当前位置:网站首页>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;
}
}
边栏推荐
- [discrete mathematics review series] II. First order logic (predicate logic)
- 22.6.7 successfully use doc2vec model to generate embedded vectors
- one
- 短文本重复率快速检测
- [discrete mathematics review series] VI. tree
- Allan variance and random error identification
- Gin blog summary 1
- 什么是CAS 以及 CAS 中的 ABA 问题
- 为doc2vec生成训练向量的数据集
- C multithreading learning note 1
猜你喜欢

还在说大学排名是笑话?最新规定:世界top50大学可以直接落户上海!

Operation of simulated examination platform for theoretical question bank of refrigeration and air conditioning equipment operation in 2022
![[discrete mathematics review series] IV. figure](/img/70/5b2f783265e7e5f6485b49088400da.png)
[discrete mathematics review series] IV. figure

Review summary of final examination of software architecture principles, methods and practices, Second Edition

Implementation of VGA protocol based on FPGA
[advanced MySQL] optimize SQL by using the execution plan explain (2)
![[Discrete Mathematical period Review Series] Second and first order Logic (precate Logic)](/img/f3/c7e61462a012ca1b88dca7b1ecdb25.png)
[Discrete Mathematical period Review Series] Second and first order Logic (precate Logic)

C multithreading learning note 2

2022 practice questions and online simulation test for the third batch of Guangdong Provincial Safety Officer a certificate (principal)

STM8S103f单片机的开发(1)LED灯的点亮
随机推荐
[special introduction] round table forum -- the integration of AI and audio and video technology
北京/上海内推 | 微软亚洲研究院系统与网络组招聘全职实习生
Redis基本使用1
什么是CAS 以及 CAS 中的 ABA 问题
C multithreading learning note 4
Primary master-slave table integration process development process
C multithreading learning note 3
微信小程序 关闭当前页面
如何写一个全局的 Notice 组件?
在启牛开户安全么
Singleton pattern and special class design
How to write a global notice component?
[notes] notes on C language array pointer, structure + two-dimensional array pointer
Win10 virtual machine download and installation process
【離散數學期複習系列】二、一階邏輯(謂詞邏輯)
Anaconda installs opencv (CV2) and uses it in the jupyter notebook
互联网公司研发效能团队为啥必须独立?何时独立?
【原创】POI 5.x XSSF和HSSF使用自定义字体颜色
Kotlin 冒泡算法 ,高德地图 过滤掉两点之间距离小于50的数据,不重复显示
one
