当前位置:网站首页>【刷题记录】21. 合并两个有序链表
【刷题记录】21. 合并两个有序链表
2022-07-25 19:11:00 【InfoQ】
一、题目描述

输入:l1 = [1,2,4], l2 = [1,3,4]
输出:[1,1,2,3,4,4]
输入:l1 = [], l2 = []
输出:[]
输入:l1 = [], l2 = [0]
输出:[0]
- 两个链表的节点数目范围是 [0, 50]
- -100 <= Node.val <= 100
- l1 和 l2 均按 非递减顺序 排列
二丶思路分析
双指针解法因为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 l1, ListNode l2) {
ListNode dummy = new ListNode(-1);
ListNode prev = dummy;
while (l1 != null && l2 != null) {
if (l1.val <= l2.val) {
prev.next = l1;
l1 = l1.next;
} else {
prev.next = l2;
l2 = l2.next;
}
prev = prev.next;
}
// 如果存在链表还没遍历完
prev.next = l1 == null ? l2 : l1;
return dummy.next;
}
}
复杂度分析
- 时间复杂度:对两条链表扫描一遍。复杂度为
m,n为两个链表的长度
- 空间复杂度:
运行结果

总结
边栏推荐
- Pymoo学习 (7):并行化Parallelization
- Basic music theory -- configuring chords
- How to change the chords after the tune of the song is changed
- Yarn installation and use tutorial [easy to understand]
- Microsoft azure and Analysys jointly released the report "Enterprise Cloud native platform driven digital transformation"
- Juzhi cloud computing opens a new era to the "proprietary cloud" of Youfu network
- The bank's wealth management subsidiary accumulates power to distribute a shares; The rectification of cash management financial products was accelerated
- Fearless of high temperature and rainstorm, how can Youfu network protect you from worry?
- Youth, oh, youth
- 小程序毕设作品之微信校园维修报修小程序毕业设计成品(4)开题报告
猜你喜欢

阿里云技术专家秦隆:可靠性保障必备——云上如何进行混沌工程?

What is the application value of MES management system

Talk about 15 tips of SQL optimization

Modelsim and quartus jointly simulate PLL FIFO and other IP cores
![[encryption weekly] has the encryption market recovered? The cold winter has not thawed yet! Check the major events in the encryption market last week!](/img/6d/b037208996ce52016d014062deaa1f.jpg)
[encryption weekly] has the encryption market recovered? The cold winter has not thawed yet! Check the major events in the encryption market last week!

果链“围城”:傍上苹果,是一场甜蜜与苦楚交错的旅途

SQL 实现 Excel 的10个常用功能,附面试原题

Baklib: make excellent product instruction manual

Korean AI team plagiarizes shock academia! One tutor with 51 students, or plagiarism recidivist

高并发下如何保证数据库和缓存双写一致性?
随机推荐
【Web技术】1391- 页面可视化搭建工具前生今世
[iniparser] simple use of the project configuration tool iniparser
Hongmeng - Damiao computing Sketchpad - VIDEO
SQL Server 2019 installation tutorial
SQL Server 2019 安装教程
聊聊接口性能优化的11个小技巧
How to prohibit the use of 360 browser (how to disable the built-in browser)
Common development software download addresses
有孚原力超算,为客户提供定制化高性能计算服务
Intouch高级报警(报警筛选)
鸿蒙-大喵计算画板-简介
基于FPGA的1080P 60Hz BT1120接口调试过程记录
歌曲转调之后和弦如何转换
[applet development] common components and basic usage details
Analysis of the internet jam in IM development? Network disconnection?
小程序毕设作品之微信校园维修报修小程序毕业设计成品(2)小程序功能
GDB help
Pymoo学习 (8):Gradients
21 days proficient in typescript-4 - type inference and semantic check
小程序毕设作品之微信校园维修报修小程序毕业设计成品(8)毕业设计论文模板