Interview Question

Interview Gurgaon, Haryana (India)

How would you traverse a linked list with n objects in such

  a way that you reach the middle of the list in less than n iterations?
Answer

Interview Answer

1 Answer

0

We use 2 pointers, increment pointer (p1) by 1 and the (p2) by 2. When the p2 reaches the end of the list then p1 is on the middle element of the list.

Interview Candidate on Aug 7, 2011

Add Answers or Comments

To comment on this, Sign In or Sign Up.