How can a cycle be detected in a singly linked list?
Question asked by visitor mahesh dixit
How can a cycle be detected in a singly linked list?
Question asked by visitor mahesh dixit
hi,
i think this logic shud work..chek it..
Take 2 pointers, p and q.
p = p -> next(increment p by 1)
q = q-> next -> next (increment q by 2)
can put this in while loop condition (p!= q)
if(p == NULL || q == NULL)
{
no loop is encountered
}
if(p == q)
{
the singly linked list has a loop
}
Hope i answered u..plz let me know if i am right