当前位置:网站首页>合并两个有序链表——LeetCode
合并两个有序链表——LeetCode
2022-04-22 15:00:00 【AllenC6】
一、链表节点
public static class ListNode {
public int val;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}
二、循环解法
思想和归并排序有点像,注意指针操作。
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode header = null; //header作为合并后的新链表的头节点
ListNode temp = null; //temp用来做新链表的操作指针,来链接新的节点
while (list1 != null && list2 != null) {
if (list1.val < list2.val) { //比较两个链表当前节点的大小,选择更小的插入新的链表末尾
if (header == null) { // 如果新链表是空的,就作为新链表的头节点
temp = list1;
header = temp;
} else { //如果新链表不为null,就链接到新链表的末尾,末尾指针用temp来维护
temp.next = list1;
temp = temp.next;
}
list1 = list1.next; //把更小的那个节点链接到新链表上之后,需要维护一下旧链表的指针让它移动到下一个待比较的节点
//我们没有创建新的节点去构造新的链表,而是直接修改旧链表的指针,这样会不会对旧链表产生影响,比如丢失指针,指针错乱等问题?
//经过分析发现,可能造成指针错乱等问题的是 temp.next = list 这句,因为它修改了旧链表next指针,仔细思考就会发现
//它动的不是当前元素的next,而是上一个元素的next,并不会影响当前元素查找下个元素
//顺带说一下指针,比如有A和B,同时指向List1这个节点,如果仅仅改变A的指向,比如A改为指向List2,这并不会影响B和List1
//如果改变A.next那B和List1的next也会改变,总结成书面语言叫啥来着我给忘了,如果长时间不接触指针突然想到这个点还真让我别扭了一下
} else {
if (header == null) {
temp = list2;
header = temp;
} else {
temp.next = list2;
temp = temp.next;
}
list2 = list2.next;
}
}
if (list1 == null) { //如果其中一个链表遍历完了,把另一个链表剩下的节点直接放到新链表的后面,要注意开始其中一个链表为null的情况
if (header == null) {
header = list2;
} else {
temp.next = list2;
}
} else {
if (header == null) {
header = list1;
} else {
temp.next = list1;
}
}
return header;
}
三、递归解法
public static ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if (list1 == null) return list2;
if (list2 == null) return list1;
ListNode headerAndTemp = null; //第一次调用,存储的是新链表头节点
if (list1.val < list2.val) {
headerAndTemp = list1;
headerAndTemp.next = mergeTwoLists(list1.next,list2); //每次都是返回更小的那个,然后通过headerAndTemp.next来维护新链表
}else {
headerAndTemp = list2;
headerAndTemp.next = mergeTwoLists(list1,list2.next);
}
return headerAndTemp; // 最后一次返回的是,第一次调用,即返回的是第一次存储的头节点
}
版权声明
本文为[AllenC6]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_37707561/article/details/124268551
边栏推荐
- 【ORB_SLAM2源码解读】分析ORB_SLAM2 RGBD 第1帧是怎么计算位置姿态的
- SMB+MSSQL
- Configure MySQL Cluster with MYCAT (2) -- configure MySQL master-slave replication
- The memory expansion application of STM32 is realized, and the single chip microcomputer with small memory can also do great things (FSMC + SRAM)
- When our son was 18 years old and went to college, we were already 60. What financial products are the most reliable for him to buy?
- C语言的基本练习(001-1)
- 记录一个sql,查询用户最后任职的一家公司,并根据企业名称+用户名称进行搜索
- [*CTF2022]oh_my_lotto_revenge
- 2021年漏洞利用状况:已公开的传统漏洞占95%
- In the second half of the smart watch, opportunities and challenges coexist
猜你喜欢
![[*CTF2022]oh_my_lotto_revenge](/img/48/d67ef80f95363e69552b1d6573eb16.png)
[*CTF2022]oh_my_lotto_revenge

运输层——运输层概述(1)

Phase I * Chapter IV * general knowledge of project management

leetcode746. 使用最小花费爬楼梯(简单)

leetcode1025. Divisor game (simple)

数据库资源负载管理(下篇)

The world reading sun will expose your book list and have the opportunity to get a free reading card!

数学——贝塞尔曲线

腾讯云IM集成(so easy)

Memcpy() function copies two-dimensional array & memcmp() function compares two-dimensional array
随机推荐
leetcode1025. Divisor game (simple)
阿里云IoT流转到postgresql数据库方案
使用 MyCat 配置 MySQL 集群(2)—— 配置 MySQL 主从复制
Share one (quick navigation)
数据库资源负载管理(下篇)
@Resources and constructors
腾讯云IM集成(so easy)
Static routing comprehensive experiment
Introduction notes to golang - redis
百合医疗IPO被终止:实控人黄凯之父黄维郭曾是佛山副市长
琢磨琢磨方法引用以及lambda--白话Lambda和方法引用
[interpretation of orb_slam2 source code] Analyze orb_ How is the position and attitude calculated in the first frame of slam2 rgbd
C语言的基本练习(001-1)
Rip introduction
When allowCredentials is true, allowedOrigins cannot contain the special value “*“ since that canno
ODPS SQL节点参数怎么设置呢?
【世界地球日】华为云云市场 | 用科技见证自然的美好改变
关于阿里云OSS资源STS访问控制
带你了解极具弹性的Spark架构的原理
STM32的内存扩展应用实现,小内存的单片机也能干大事(FSMC+SRAM)
