当前位置:网站首页>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~
边栏推荐
- Common functions of pytorch
- 聪明人的游戏提高篇:第三章第二课:“桐桐数”(number)
- go里面的基本知识
- 51 microcontroller peripherals article: dot-matrix LCD
- What are the ways to improve software testing capabilities?After reading this article, it will take you up a notch
- 国际顶会OSDI首度收录淘宝系统论文,端云协同智能获大会主旨演讲推荐
- C语言中i++和++i在循环中的差异性
- zabbix邮件报警和微信报警
- Linux CentOS8安装Redis6
- The virtual reality real estate display system foresees the future decoration effect in advance
猜你喜欢
What is the most important ability of a programmer?
代码编世界 科技创未来
Install and use Google Chrome
深度学习——CNN实现MNIST手写数字的识别
Machine learning -- - theory of support vector machine (SVM)
51单片机外设篇:点阵式LCD
虚拟现实房产展示系统提前预见未来装修效果
51 microcontroller peripherals article: dot-matrix LCD
上海交大牵手淘宝成立媒体计算实验室:推动视频超分等关键技术发展
触发器简单解释
随机推荐
51单片机外设篇:点阵式LCD
Mysql数据库 | 基于Docker搭建Mysql-8.0以上版本主从实例实战
HCIP第十七天
C语言小游戏——扫雷小游戏
Point Density-Aware Voxels for LiDAR 3D Object Detection 论文笔记
使用TinkerPop框架对GDB增删改查
Luogu mini game Daquan (everyone who uses Luogu must know)
Automated operation and maintenance tools - ansible, overview, installation, module introduction
zabbix自动发现和自动注册
驱动页面性能优化的3个有效策略
leetcode-338.比特位计数
Redis集群模式
字节面试题:如何保证缓存和数据库的一致性
VMTK环境配置记录
【合集- 行业解决方案】如何搭建高性能的数据加速与数据编排平台
pytorch basic operations: classification tasks using neural networks
TikTok平台的两种账户有什么区别?
Machine learning -- - theory of support vector machine (SVM)
Difference and analysis of CPU usage and load
Redis-cluster mode (master-slave replication mode, sentinel mode, clustering mode)