当前位置:网站首页>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~
边栏推荐
猜你喜欢

软件测试的需求人才越来越多,为什么大家还是不太愿意走软件测试的道路?

国际顶会OSDI首度收录淘宝系统论文,端云协同智能获大会主旨演讲推荐

腾讯大咖分享 | 腾讯Alluxio(DOP)在金融场景的落地与优化实践

整合ssm(一)

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

Thread Basics (1)

pl/sql之神奇的嵌套与变量生命周期

机器学习——支持向量机原理

MySQL数据表的基本操作和基于 MySQL数据表的基本操作的综合实例项目

Redis-cluster mode (master-slave replication mode, sentinel mode, clustering mode)
随机推荐
51单片机外设篇:ADC
DNS的解析流程
Deep learning - CNN realizes the recognition of MNIST handwritten digits
HCIP BGP综合实验 建立对等体、路由反射器、联邦、路由宣告及聚合
Shell 脚本不同玩法
kubernetes affinity, anti-affinity, taint, tolerance
如何优化OpenSumi终端性能?
51单片机外设篇:点阵式LCD
Redis(十二) - Redis消息队列
JUC(一)- JUC学习概览 - 对JUC有一个整体的认识
zabbix邮件报警和微信报警
There are more and more talents in software testing. Why are people still reluctant to take the road of software testing?
golang generics
go里面的基本知识
kubernetes 亲和、反亲和、污点、容忍
Polar Parametrization for Vision-based Surround-View 3D Detection 论文笔记
关于web应用的目录结构
C竞赛训练
OAuth 授权协议 | 都云原生时代了,我们应该多懂一点OAuth ?
虚拟现实房产展示系统提前预见未来装修效果