File tree Expand file tree Collapse file tree 3 files changed +15
-12
lines changed
Expand file tree Collapse file tree 3 files changed +15
-12
lines changed Original file line number Diff line number Diff line change 3939
4040<div align =" center " ><strong >最新文章会首发在公众号「代码随想录」,扫码看看吧,你会发现相见恨晚!</strong ></img ></div >
4141
42- <div align =" center " ><img src =' https://img-blog.csdnimg.cn/20210321101634295 .jpg' width =150 alt =' ' > </img ></div >
42+ <div align =" center " ><img src =' ./pics/公众号二维码 .jpg' width =150 alt =' ' > </img ></div >
4343
4444## 如何使用该刷题攻略
4545
Original file line number Diff line number Diff line change 66</p >
77
88
9- # 142.环形链表II
9+
10+ > 找到有没有环已经很不容易了,还要让我找到环的入口?
11+
12+
13+ ## 142.环形链表II
1014
1115https://leetcode-cn.com/problems/linked-list-cycle-ii/
1216
@@ -19,7 +23,7 @@ https://leetcode-cn.com/problems/linked-list-cycle-ii/
1923
2024![ 循环链表] ( https://img-blog.csdnimg.cn/20200816110112704.png )
2125
22- # 思路
26+ ## 思路
2327
2428这道题目,不仅考察对链表的操作,而且还需要一些数学运算。
2529
@@ -28,7 +32,7 @@ https://leetcode-cn.com/problems/linked-list-cycle-ii/
2832* 判断链表是否环
2933* 如果有环,如何找到这个环的入口
3034
31- ## 判断链表是否有环
35+ ### 判断链表是否有环
3236
3337可以使用快慢指针法, 分别定义 fast 和 slow指针,从头结点出发,fast指针每次移动两个节点,slow指针每次移动一个节点,如果 fast 和 slow指针在途中相遇 ,说明这个链表有环。
3438
@@ -54,7 +58,7 @@ fast和slow各自再走一步, fast和slow就相遇了
5458![ 141.环形链表] ( https://tva1.sinaimg.cn/large/008eGmZEly1goo4xglk9yg30fs0b6u0x.gif )
5559
5660
57- ## 如果有环,如何找到这个环的入口
61+ ### 如果有环,如何找到这个环的入口
5862
5963** 此时已经可以判断链表是否有环了,那么接下来要找这个环的入口了。**
6064
@@ -102,10 +106,9 @@ fast指针走过的节点数:` x + y + n (y + z)`,n为fast指针在环内走
102106
103107其实这种情况和n为1的时候 效果是一样的,一样可以通过这个方法找到 环形的入口节点,只不过,index1 指针在环里 多转了(n-1)圈,然后再遇到index2,相遇点依然是环形的入口节点。
104108
109+ 代码如下:
105110
106- # C++代码
107-
108- ```
111+ ``` C++
109112/* *
110113 * Definition for singly-linked list.
111114 * struct ListNode {
@@ -173,13 +176,14 @@ public:
173176
174177好了,这次把为什么第一次在环中相遇,slow的 步数 是 x+y 而不是 x + 若干环的长度 + y ,用数学推理了一下,算是对[链表:环找到了,那入口呢?](https://mp.weixin.qq.com/s/_QVP3IkRZWx9zIpQRgajzA)的补充。
175178
176- # 总结
179+ ## 总结
177180
178181这次可以说把环形链表这道题目的各个细节,完完整整的证明了一遍,说这是全网最详细讲解不为过吧,哈哈。
179182
180- ## 其他语言补充
183+ ## 其他语言版本
184+
185+ python:
181186
182- python
183187```python
184188class Solution:
185189 def detectCycle(self, head: ListNode) -> ListNode:
@@ -200,7 +204,6 @@ class Solution:
200204 return None
201205```
202206
203- 欢迎大家补充其他语言的版本实现哈
204207
205208
206209
You can’t perform that action at this time.
0 commit comments