https://leetcode.cn/problems/c32eOV/
我们使用两个指针,fast 与 slow。它们起始都位于链表的头部。随后slow 指针每次向后移动一个位置,而fast 指针向后移动两个位置。如果链表中存在环,则fast 指针最终将再次与slow 指针在环中相遇。
struct ListNode *detectCycle(struct ListNode *head) {struct ListNode *fast = head, *slow = head;while(fast && fast->next){slow = slow->next;fast = fast->next->next;if(fast == slow){struct ListNode * meet = slow;while(head != meet){head = head->next;meet = meet->next;}return meet;}}return NULL;
}