top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

If we have pointer to head of a linked list, suggest ways to jump to the middle element of the linked list?

+2 votes
540 views
If we have pointer to head of a linked list, suggest ways to jump to the middle element of the linked list?
posted Sep 26, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

3 Answers

+1 vote
 
Best answer

With one step(O(1)), we cannot jump to middle of a link list whether its singly or doubly link list.
In my opinion, the best way is :
Traverse linked list using two pointers. Move one pointer by one and other pointer by two. When the fast pointer reaches end slow pointer will reach middle of the linked list.

Lets wait for others to answer, if something we can find better :)

answer Sep 26, 2014 by Aarti Jain
0 votes

We can traverse the whole linked list and count the no. of nodes. Now traverse the list again till count/2 and return the node at count/2.

answer Feb 15, 2016 by Ajay Kumar Topno
0 votes

count number of element and then divide by two. start the loop from till the getting value and reach on middle

answer Mar 14, 2016 by Ankit Tiwari
...