Consider the following code which is used to detect the loop in the linked list. Find the missing statement A?
p = head;
q = head->next;
while(A)
{
if(p == q) exit(0) //loop detected
p = p->next;
q = (q->next)?(q->next->next) : q->next;
}
Answer: (p!=NULL) && (q!=NULL)
My question is: Why we are using (p!=NULL). I don't think it is even required. only q is sufficient. As it is running faster as compared to p, it will reach first to the end of the linked list. my answer is q!=NULL and it is in one of the options. Please correct me if I am wrong.