class Solution {
public:
ListNode *detectCycle(ListNode *head) {
auto fast = head;
auto slow = head;
while(fast){
fast = fast -> next;
slow = slow -> next;
if (fast) fast = fast -> next;
else break;
}
if (fast == NULL) return Fasle;
else return True;
// LeetCode 142. 环形链表 II
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
auto fast = head;
auto slow = head;
while(fast){
fast = fast -> next;
slow = slow -> next;
if (fast) fast = fast -> next;
else break;
if (fast == slow){ // 大神的思路,我服了,当快慢指针相遇时,让慢指针回到开头,此后快慢指针每次都再走一个结点,再相遇的点就是环路开始的结点。
slow = head;
while(slow != fast){
slow = slow -> next;
fast = fast -> next;
}
return fast;
}
}
return NULL;
}
};