/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* slow = head;
ListNode* fast = head;
ListNode* node = head;
// int num_s=0;
// int num_f=0;
if(node==NULL) return node;
while(slow->next!=NULL&&fast->next!=NULL&&fast->next->next!=NULL){
slow=slow->next;
// num_s++;
fast=fast->next->next;
// num_f=num_f+2;
if(slow==fast){ // 表示有环
// return num_f-num_s-2;
while(node!=slow)
{ node=node->next;
slow=slow->next; // 画图可知
}
return node;
}
}
//2*(dan+huan_slow)=dan+huan_slow+huan
//dan+huan_slow=huan
//num_f=2*num_s
//num_f-num_s=num_s=
return NULL;
}
};