当前位置:网站首页>leetcode solves the linked list merge problem in one step
leetcode solves the linked list merge problem in one step
2022-08-02 06:35:00 【Do you eat pancakes?】
1、合并两个有序链表
题目描述:

思路:Merging two ordered linked lists is also a very classic problem,看到这道题目,Many of my friends will probably feel a sense of déjà vu,是不是有点像,Merge two arrays hahahaha,So let's use the dumb method first(依次比较)l来试试
1、依次比较
看看代码:
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode cur = new ListNode(0);
ListNode result = cur;
ListNode temp1 = list1;
ListNode temp2 = list2;
while (temp1 != null && temp2 != null){
if (temp1.val < temp2.val){
cur.next = temp1;
temp1 = temp1.next;
cur = cur.next;
}else {
cur.next = temp2;
temp2 = temp2.next;
cur = cur.next;
}
}
if (temp1 == null){
while (temp2 != null){
cur.next = temp2;
temp2 = temp2.next;
cur = cur.next;
}
}else if (temp2 == null){
while (temp1 != null){
cur.next = temp1;
temp1 = temp1.next;
cur = cur.next;
}
}
return result.next;
}时间复杂度:O(n)
空间复杂度:O(1)
2、递归
思路:Since it is recursion, we will think of the three recursive elements of this question
- 终止条件:list1为空,或者list2为空
- 返回值:Each layer returns a sorted linked list
- Native recursive content:如果list1.val更小,则将list1.nextJoin with the next level sorted linked list;list2反之亦然
我们看看代码:
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if (list1 == null) return list2;
if (list2 == null) return list1;
if (list1.val<list2.val){
list1.next = mergeTwoLists(list1.next,list2);
return list1;
}else {
list2.next = mergeTwoLists(list1,list2.next);
return list2;
}
}时间复杂度:O(m+n),m为list1的长度,n为list2的长度
2、合并k个升序链表
题目描述:

思路:拿到题目,First, let's examine the question,合并k条,Essentially and merge2A linked list in ascending order makes no difference,we just need to split,把k条链表,拆分成若干个2条链表,Thinking moment is clear!
至于如何拆分,I don't know if you all know 归并排序 ,If you don't know, you can move归并排序
The idea used in merge sort is to divide and conquer,先分后治,先拆分,后合并,Something to do with this problem we have the same effect,split the list first,merge the linked list;The only difference is when merge sort,We are splitting the same array,And now we are splitkdifferent linked lists,But we want the same result!
了解完之后,Let's look at the code:
public ListNode mergeKLists(ListNode[] lists) {
if (lists.length < 1) return null;
return mergeLists(lists, 0, lists.length);
}
public ListNode mergeLists(ListNode[] lists, int left, int right) {
if (left >= right) return lists[left];
int mid = (left + right) / 2;
//先分
ListNode l1 = mergeLists(lists, left, mid);
ListNode l2 = mergeLists(lists, mid + 1, right);
//后治
return mergeTwoLists(l1, l2);
}
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if (list1 == null) return list2;
if (list2 == null) return list1;
if (list1.val < list2.val) {
list1.next = mergeTwoLists(list1.next, list2);
return list1;
} else {
list2.next = mergeTwoLists(list1, list2.next);
return list2;
}
}Here in order to optimize the time complexity of our merging algorithm,We can use an iterative algorithm,That is first question of the first one to replace the merger recursive method,will speed up a lot of time
public ListNode mergeKLists(ListNode[] lists) {
if (lists.length < 1) return null;
return mergeLists(lists, 0, lists.length);
}
public ListNode mergeLists(ListNode[] lists, int left, int right) {
if (left >= right) return lists[left];
int mid = (left + right) / 2;
//先分
ListNode l1 = mergeLists(lists, left, mid);
ListNode l2 = mergeLists(lists, mid + 1, right);
//后治
return mergeTwoLists(l1, l2);
}
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode cur = new ListNode(0);
ListNode result = cur;
ListNode temp1 = list1;
ListNode temp2 = list2;
while (temp1 != null && temp2 != null){
if (temp1.val < temp2.val){
cur.next = temp1;
temp1 = temp1.next;
cur = cur.next;
}else {
cur.next = temp2;
temp2 = temp2.next;
cur = cur.next;
}
}
if (temp1 == null){
while (temp2 != null){
cur.next = temp2;
temp2 = temp2.next;
cur = cur.next;
}
}else if (temp2 == null){
while (temp1 != null){
cur.next = temp1;
temp1 = temp1.next;
cur = cur.next;
}
}
return result.next;
}总结:合并升序链表,It is also a frequently asked question in the written test.,Everyone needs to learn~
边栏推荐
- flex布局(弹性布局)
- 51 MCU peripherals: DS18B20
- Differences between i++ and ++i in loops in C language
- 保证家里和企业中的WIFI安全-附AC与AP组网实验
- 程序员写PPT的小技巧
- pl/sql之神奇的嵌套与变量生命周期
- 软件测试的需求人才越来越多,为什么大家还是不太愿意走软件测试的道路?
- 国际顶会OSDI首度收录淘宝系统论文,端云协同智能获大会主旨演讲推荐
- Point Density-Aware Voxels for LiDAR 3D Object Detection Paper Notes
- selenium + robotframework的运行原理
猜你喜欢

Important concepts of target detection - IOU, receptive field, hole convolution, mAP

25K测试老鸟6年经验的面试心得,四种公司、四种问题…

5年在职经验之谈:2年功能测试、3年自动化测试,从入门到不可自拔...

测试技术之APP蓝牙连接测试

HCIP BGP综合实验 建立对等体、路由反射器、联邦、路由宣告及聚合

Mysql数据库 | 基于Docker搭建Mysql-8.0以上版本主从实例实战

go里面的基本知识

C语言操作符详解(2)

Stress testing and performance analysis of node projects

秒杀系统小demo
随机推荐
Install and use Google Chrome
The virtual reality real estate display system foresees the future decoration effect in advance
机器学习——支持向量机原理
Smart people's game improvement: Chapter 3, Lesson 2: "Number of Tongtong" (number)
DNS的解析流程
驱动页面性能优化的3个有效策略
Luogu mini game Daquan (everyone who uses Luogu must know)
国际顶会OSDI首度收录淘宝系统论文,端云协同智能获大会主旨演讲推荐
Home NAS server (4) | MergerFS and SnapRaid data backup
字节面试题:如何保证缓存和数据库的一致性
淘系资深工程师整理的300+项学习资源清单(2021最新版)
Detailed explanation of interface in Go language
深入剖析成员变量和局部变量的初始化问题
程序员最重要的能力是什么?
关于鸿蒙系统 JS UI 框架源码的分析
Constructors, member variables, local variables
APP Bluetooth connection test of test technology
目标检测重要概念——IOU、感受野、空洞卷积、mAP
The advantages of making web3d dynamic product display
Deep learning - CNN realizes the recognition of MNIST handwritten digits