So you can do it in 50 iterations with a little hack. Keep another head(*head2
) which points to head->link
. This will still take constant space. Compare it with the current node in the if condition along with the original head. See the code below--
struct node *head, *head2, *temp;
temp = head; //assuming head points to start of the list
head2 = head->link;
while(i != 50) {
temp = temp->link->link;
if(temp==head || temp==head2) {
printf("circular");
break;
}
i++;
}