edited by
596 views
1 votes
1 votes

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.

edited by

Please log in or register to answer this question.

Related questions

0 votes
0 votes
1 answer
1
2 votes
2 votes
0 answers
2
1 votes
1 votes
1 answer
4
Pradip Nichite asked Jan 22, 2016
695 views
Here, what will be the Head->Next , I am confused is it first element 50 or second element 29.