This is known as the "tortoise and hare" algorithm. Imagine p is
running ahead of the q pointer, at a certain point they will meet
inside a loop if it exists.

On Dec 1, 3:12 am, Vijay Khandar <vijaykhand...@gmail.com> wrote:
> What does the following program do on the singly linked list?
>
> p=head;
> q=head->next;
> while(p!=null && q!null)
> {
> if(p==q)
> {
> exit(0)}
>
> p=p->next;
> q=(q->next)?(q->next->next):q->next;
>
> }
>
> a)traverse the entire singly linked list
> b)detects the duplicate nodes
> c)detects the loop in singly linked list
> d)detects the duplicate nodes at alternate places
>
> plz explain anyone with correct option..........

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to