Can someone please explain Floyd algorithm with this example. It is not terminating for me and is the algorithm implemented complete ?.
Is something wrong with my code? Code is as follows:
Node* FindLoopBegin(Node *head){
Node *slowptr = head,*fastptr = head;
bool LoopExists = false;
while(slowptr && fastptr){
fastptr = fastptr->next;
if(fastptr == slowptr) {LoopExists = true;break;}
if(fastptr == NULL) {LoopExists = false; return NULL;}
fastptr = fastptr->next;
if(fastptr == slowptr) {LoopExists = true;break;}
slowptr = slowptr->next;
}
if(LoopExists) {
slowptr = head;
while(slowptr != fastptr){
slowptr = slowptr->next;
fastptr = fastptr->next;
}
return slowptr;
}
return NULL;
}
Apologies for the bad drawing!