You can detect a loop in a linked list by using below logic:
int find_loop(node *head){
node *next=head;
node *temp;
for( temp=head;temp!=NULL;temp=temp->ptr){
temp=temp->ptr;
if(temp->ptr!=NULL)
temp=temp->ptr;
next=next->ptr;
if(temp==next)
printf("Loop");
return 0;
}
printf("No Loop")
return 0;
}
To find a circle:
int find_circle(node *head){
node *temp;
for( temp=head;temp!=NULL;temp=temp->ptr){
temp=temp->ptr;
if(temp==head)
printf("Circle")
return 0;
}
printf("No Circle")
return 0;
}