/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode *detectCycle(struct ListNode *head) { struct ListNode *fast,*slow; bool flag; fast=slow=head,flag=false; while(fast&&slow){ if(fast)fast=fast->next; if(fast)fast=fast->next; if(slow)slow=slow->next; if(fast==slow&&fast!=NULL){ flag=true; break; } } if(flag==false)return NULL; slow=head; while(slow!=fast){ slow=slow->next; fast=fast->next; } return slow; }