当前位置:网站首页>LeetCode 142. 环形链表 II
LeetCode 142. 环形链表 II
2022-06-27 00:11:00 【碳烤小肥羊。。。】
题目描述:给定一个链表的头节点 head ,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。不允许修改 链表。
方法一:使用map集合
一个非常直观的思路是:声明一个map集合(key:结点, value:记录结点位置),然后遍历链表中的每个节点,判断该结点是否在于map集合中,如果不存在将该结点加入到map集合中,如果存在,说明该链表存在环,返回该结点即可。
/** * 使用map集合 * @param head * @return */
public static ListNode detectCycle(ListNode head){
if(head == null || head.next == null){
return null;
}
Map<ListNode, Integer> map = new HashMap<>();
int pos = 0; // 初始索引为0
while (head != null){
if(map.containsKey(head)){
return head;
}
map.put(head, pos++);
head = head.next;
}
// head == null跳出while循环,说明链表不含链
return null;
}
方法二:快慢指针
我们使用两个指针,fast 与 slow。它们起始都位于链表的头部。随后,slow 指针每次向后移动一个位置,而 fast指针向后移动两个位置。如果链表中存在环,则 fast指针最终将再次与slow 指针在环中相遇。
如下图所示,设链表中环外部分的长度为 x。slow 指针进入环后,又走了 y 的距离与 fast 相遇。此时,fast指针已经走完了环的 n 圈,因此它走过的总距离为 x + n ( y + z ) + y = x + ( n + 1 ) y + z 。 x + n(y + z) + y = x + (n+1)y + z。 x+n(y+z)+y=x+(n+1)y+z。
根据题意,任意时刻,fast 指针走过的距离都为 slow 指针的 2 倍。因此,我们有 x + ( n + 1 ) y + z = 2 ( x + y ) * x = z + ( n − 1 ) ( y + z ) x + (n+1)y + z = 2(x + y) * x=z+(n−1)(y+z) x+(n+1)y+z=2(x+y) * x=z+(n−1)(y+z)
有了这个等量关系,我们会发现:从相遇点到入环点的距离加上 n−1 圈的环长,恰好等于从链表头部到入环点的距离。
因此,当发现 slow 与 fast相遇时,我们再额外使用两个指针index1, index2。起始,它们分别指向链表头部和slow与fast指针相遇点;随后,index1和index2 每次向后移动一个位置。最终,它们会在入环点相遇。
/** * 快慢指针思想 * @param head * @return */
public static ListNode detectCycle(ListNode head){
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null){
slow = slow.next; // slow每次走一步
fast = fast.next.next; // fast每次走两步
if(fast == slow){
// 有环
ListNode index1 = fast;
ListNode index2 = head;
// 两个指针, 从头节点和相遇节点,各走一步,直到相遇,相遇点即为环的入口
while (index1 != index2){
index1 = index1.next;
index2 = index2.next;
}
return index1;
}
}
return null;
}
思考问题1:在存在环的情况下,为何慢指针一定会和快指针相遇?
- 可以认为快指针和慢指针是相对运动的,假设慢指针的速度是 1节点/秒,快指针的速度是 2节点/秒,当以慢指针为参考系的话(即慢指针静止),快指针的移动速度就是 1节点/秒,所以肯定会相遇。
思考问题2:为什么在第一圈就会相遇呢?
- 设环的长度为 L,当慢指针刚进入环时,慢指针需要走 L 步(即 L 秒)才能走完一圈,此时快指针距离慢指针的最大距离为 L-1,我们再次以慢指针为参考系,如上所说,快指针在按照1节点/秒的速度在追赶慢指针,所以肯定能在 L 秒内追赶到慢指针。
边栏推荐
- Safe and cost-effective payment in Thailand
- 简单快速的数网络(网络中的网络套娃)
- Pet hospital management system based on SSMP
- JS library for number formatting
- Timing mechanism of LwIP
- Amway! How to provide high-quality issue? That's what Xueba wrote!
- My advanced learning notes of C language ----- keywords
- JSON解析,ESP32轻松获取时间气温和天气
- These 10 copywriting artifacts help you speed up the code. Are you still worried that you can't write a copywriting for US media?
- 大赛报名 | AI+科学计算重点赛事之一——中国开源科学软件创意大赛,角逐十万奖金!
猜你喜欢
Moher College - SQL injection vulnerability test (error reporting and blind note)
Technical dry goods | what is a big model? Oversized model? Foundation Model?
Safe and cost-effective payment in Thailand
com.fasterxml.jackson.databind.exc.MismatchedInputException: Expected array or string. at [Source:x
滑环选型选购时需要注意的技巧
When transformer encounters partial differential equation solution
这3个并发编程的核心,竟然还有人不知道?
自定义MVC(导成jar包)+与三层架构的区别+反射+面试题
Other service registration and discovery
BootstrapBlazor + FreeSql实战 Chart 图表使用(2)
随机推荐
[vscade] preview MD file
Kubernetes visual interface dashboard
自定义JSP[if,foreach,数据,select]标签
com.fasterxml.jackson.databind.exc.MismatchedInputException: Expected array or string. at [Source:x
Big guys talk about the experience sharing of the operation of the cutting-edge mindspore open source community. Come up with a small notebook!
Competition Registration | one of the key ai+ scientific computing competitions - China open source scientific software creativity competition, competing for 100000 bonus!
解决STC8G1K08程序不能运行的问题和端口配置
JS library for number formatting
中信证券佣金 网上开户炒股安全吗
直播回顾 | 子芽&CCF TF:云原生场景下软件供应链风险治理技术浅谈
Understanding of "the eigenvectors corresponding to different eigenvalues cannot be orthogonalized"
Memorizing byte order of big and small end
How to easily describe the process of machine learning?
What is the difference between the working principle of gas-liquid slip ring and other slip rings
温故知新--常温常新
Can I open an account for stock trading on my mobile phone? Is it safe to open an account for stock trading on the Internet
Interface test framework practice (I) | requests and interface request construction
光谱共焦如何测量玻璃基板厚度
MATLAB data type - character type
Batch generate folders based on file names