Data Structure Linked list Question:
Download Questions PDF

Tell me how to find middle element of linked list in one pass?

Linked list Interview Question
Linked list Interview Question

Answer:

One of the most popular question from data structures and algorithm, mostly asked on telephonic interview. Since many programmer know that, in order to find length of linked list we need to first traverse through linkedlist till we find last node, which is pointing to null, and then in second pass we can find middle element by traversing only half of length. They get confused when interviewer ask him to do same job in one pass. In order to find middle element of linked list in one pass you need to maintain two pointer, one increment at each node while other increments after two nodes at a time, by having this arrangement, when first pointer reaches end, second pointer will point to middle element of linked list. See this trick to find middle element of linked list in single pass.

Download Linked list Interview Questions And Answers PDF

Previous QuestionNext Question
Do you know what does the following function do for a given Linked List?Do you know how to find if linked list has loop?