CS-Notes/docs/notes/23. 链表中环的入口结点.md
2019-11-02 17:33:10 +08:00

41 lines
1.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 23. 链表中环的入口结点
[NowCoder](https://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4?tpId=13&tqId=11208&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github)
## 题目描述
一个链表中包含环请找出该链表的环的入口结点要求不能使用额外的空间
## 解题思路
使用双指针一个指针 fast 每次移动两个节点一个指针 slow 每次移动一个节点因为存在环所以两个指针必定相遇在环中的某个节点上假设相遇点在下图的 z1 位置此时 fast 移动的节点数为 x+2y+zslow x+y由于 fast 速度比 slow 快一倍因此 x+2y+z=2(x+y)得到 x=z
在相遇点slow 要到环的入口点还需要移动 z 个节点如果让 fast 重新从头开始移动并且速度变为每次移动一个节点那么它到环入口点还需要移动 x 个节点在上面已经推导出 x=z因此 fast slow 将在环入口点相遇
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/bb7fc182-98c2-4860-8ea3-630e27a5f29f.png" width="500"/> </div><br>
```java
public ListNode EntryNodeOfLoop(ListNode pHead) {
if (pHead == null || pHead.next == null)
return null;
ListNode slow = pHead, fast = pHead;
do {
fast = fast.next.next;
slow = slow.next;
} while (slow != fast);
fast = pHead;
while (slow != fast) {
slow = slow.next;
fast = fast.next;
}
return slow;
}
```
<div align="center"><img width="320px" src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/githubio/公众号二维码-2.png"></img></div>