https://leetcode.cn/problems/c32eOV/solutions/977000/jian-zhi-offer-ii-022-lian-biao-zhong-hu-8f1m/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
// 环形入口节点,第一次相遇后,将快指针从头节点开始,每次走一步,当
// 再次与slow指针相遇,则就是入口节点
ListNode *detectCycle(ListNode *head) {
if (head == nullptr || head->next == nullptr) return nullptr;
ListNode* fast = head;
ListNode* slow = head;
while(true) {
if (fast == nullptr || fast->next == nullptr) return nullptr;
slow = slow->next;
fast = fast->next->next;
if (fast == slow) break;
}
fast = head;
while(slow != fast) {
slow = slow->next;
fast = fast->next;
}
return fast;
}
};