Program to Print Middle Element of a Linked List

Write a program to print middle element of a linked list. It is also the most asked question in interviews.  I assume you have a basic understanding of linked list and it’s concept.

First Method:

i) Traverse a linked list and maintain the count of nodes.

ii) After traversing a linked list we know it’s length. In a second traversal, traverse the linked list by count/2 and print the last element(count/2). We get the middle element of a linked list.

The above method works good. But you can solve this problem in one pass, by using two pointers.

Subscribe Our Tutorials

Get Latest Updates on Facebook

Program to Reverse a Linked List

Second Method:

i) Take two pointers. Move the first pointer by one and second pointer by two.

ii) When the second pointer reaches at the end of a linked list. The first pointer points to the middle element of a linked list.

Program to Print Middle Element of a Linked List

Books for Data Structure Interview Questions

Data Structure Books on Flipkart

WebRewrite

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.
Tagged , , , . Bookmark the permalink.