1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
class Solution { public: ListNode *detectCycle(ListNode *head) { ListNode *fast=head; ListNode *slow=head; while(fast!=NULL&&fast->next!=NULL){ fast=fast->next->next; slow=slow->next; if(fast==slow){ ListNode* index1=fast; ListNode* index2=head; while(index1!=index2){ index1=index1->next; index2=index2->next; } return index2; } } return NULL; } };
|