Find Middle Element of a Linked List – Java Code

In this tutorial, We are going to explain how to find middle element of a linked list in Java. This programming question is mostly asked in a technical interviews.

For example: Given a linked list, the middle element in this linked list is 8.

15 -> 9 -> 8 -> 5 -> 6 -> NULL

There are multiple approaches to solve this problem. In this tutorial, We are going to learn two approaches through which we can find middle element of a singly linked list. Also, i have also added video tutorial at the end of this tutorial.

How to Find Middle Element of a Linked List?

Method 1:

i) Traverse and find the length of a linked list.
ii) Once we know the length of a linked list. We can traverse again upto length/2 and print the value of a node present at length/2.

Can we do better than that. Yes we can find middle element of a singly linked list in single pass. Let’s discuss the second approach.

Method 2: Find middle element of a linked list in single traversal.

i) Use two pointer slow and fast and initialized both to head of a linked list.
ii) Move fast pointer by two step and slow pointer by one step in each iteration.
iii) When fast pointer reaches at the end of a linked list, The slow pointer will reach to the middle  of a linked list.

Subscribe Our Tutorials

Get Latest Updates on Facebook

Find Middle Element of a Linked List

Find Middle Element of a Linked List

Java Program to Find Middle Element of a Linked List in One Pass

We have discussed two approaches to solve this problem. In this java code example, we are going to implement second approach in which we find middle element of a linked list in single pass.

In this video tutorial, I have explained how to find middle element of a linked list in single pass.

About WebRewrite

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