Skip to content

Commit 8ed7416

Browse files
committed
auto commit
1 parent 5b60fe9 commit 8ed7416

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed
Loading

docs/notes/剑指 offer 题解.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1197,11 +1197,12 @@ public ListNode FindKthToTail(ListNode head, int k) {
11971197

11981198
## 解题思路
11991199

1200-
使用双指针,一个指针 fast 每次移动两个节点,一个指针 slow 每次移动一个节点。因为存在环,所以两个指针必定相遇在环中的某个节点上。假设相遇点在下图的 y6 位置,此时 fast 移动的节点数为 x+2y+z,slow 为 x+y,由于 fast 速度比 slow 快一倍,因此 x+2y+z=2(x+y),得到 x=z。
1200+
使用双指针,一个指针 fast 每次移动两个节点,一个指针 slow 每次移动一个节点。因为存在环,所以两个指针必定相遇在环中的某个节点上。假设相遇点在下图的 z1 位置,此时 fast 移动的节点数为 x+2y+z,slow 为 x+y,由于 fast 速度比 slow 快一倍,因此 x+2y+z=2(x+y),得到 x=z。
12011201

12021202
在相遇点,slow 要到环的入口点还需要移动 z 个节点,如果让 fast 重新从头开始移动,并且速度变为每次移动一个节点,那么它到环入口点还需要移动 x 个节点。在上面已经推导出 x=z,因此 fast 和 slow 将在环入口点相遇。
12031203

1204-
<div align="center"> <img src="pics/70fa1f83-dae7-456d-b94b-ce28963b2ba1.png" width="500"/> </div><br>
1204+
<div align="center"> <img src="pics/d5d3b7ae-2712-412e-98f1-633ce6ec5955.png" width="500"/> </div><br>
1205+
12051206

12061207
```java
12071208
public ListNode EntryNodeOfLoop(ListNode pHead) {

0 commit comments

Comments
 (0)