Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ListNode *detectCycle(ListNode *head) {
- if(!head||!head->next)return NULL;
- ListNode*slow=head,*fast=head;
- int flag=0;
- while(slow!=NULL&&fast!=NULL)
- {
- if(!fast||!fast->next)return NULL;
- slow=slow->next;
- fast=fast->next->next;
- if(slow==fast){flag=1; break;}
- }
- if(flag==0)return NULL;
- slow=head;
- while(slow!=fast)
- {
- slow=slow->next;
- fast=fast->next;
- }
- return slow;
- }
Add Comment
Please, Sign In to add comment