9.Linked List Cycle
Problems related to Cycle Detection in Linked List
Determine if a Linked List has a cycle in it.
bool detectCycle()
{
bool res = false;
if (head == NULL)
{
return false;
}
node *p = head;
node *q = head;
while (p->next != NULL && p->next->next != NULL)
{
q = q->next;
p = p->next->next;
if (q == p)
{
res = true;
break;
}
}
return res;
}Determine the node where the cycle begins
Determine the length of the cycle
Last updated