1 ListNode *detectCycle(ListNode *head) { 2 if(!head||!head->next) 3 return NULL; 4 ListNode *fast,*slow; 5 fast=head; 6 slow=head; 7 while(fast&&fast->next) 8 { 9 slow=slow->next; 10 fast=fast->next->next; 11 if(fast==slow) 12 { 13 fast=head; 14 while(fast!=slow) 15 { 16 fast=fast->next; 17 slow=slow->next; 18 } 19 return fast; 20 } 21 } 22 return NULL; 23 }
AC