Find middle node/element of a LinkedList in Java in one pass.

Write a program to find/print middle element of a Linked list in Java in one pass.

Solution:

1)      Initialize 2 pointers called fast and slow with header element. Move slow pointer by one element and fast pointer by two elements

2)      Once the fast pointer reaches end, then slow pointer will reach middle of a linked list. 

 Output:

 

Posted in data-structures and tagged , , .

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">