Find middle element of a linked list in java

If you want to practice data structure and algorithm programs, you can go through data structure and algorithm interview questions.
This is one of the popular interview question.
In this post, we will discuss how to find middle element in linkedlist in most efficient way.

Java Linked List Interview Programs:

    How to reverse a linked list in java How to reverse a linked list in pairs in java How to find middle element of linked list in java How to detect a loop in linked list in java Find start node of loop in linkedlist How to find nth element from end of linked list How to check if linked list is palindrome in java Add two numbers represented by linked list in java


I am not using java LinkedList API here. If you use that API, you can directly find size of linkedlist using size() function and then locate length/2.

One of the algo for this would be:

  • Traverse the list and find the length of list
  • After finding length, again traverse the list and locate n/2 element from head of linkedlist.
Time complexity=time for finding length of list + time for locating middle element=o(n)+o(n) =o(n)
Space complexity= o(1).

Efficient approach:

Above approach will take two traversal but we can find middle element in one traversal also using following algo:
  1. Use two pointer fastptr and slowptr and initialize both to head of linkedlist
  2. Move fastptr by two nodes and slowptr by one node in each iteration.
  3. When fastptr reaches end of nodes, the slowptr pointer will be  pointing to middle element.
 Lets create java program for it:
package org.arpit.java2blog;

public class LinkedList{

 private Node head;

 private static class Node {
  private int value;
  private Node next;

  Node(int value) {
   this.value = value;


 public void addToTheLast(Node node) {

  if (head == null) {
   head = node;
  } else {
   Node temp = head;
   while ( != null)
    temp =; = node;

 public void printList() {
  Node temp = head;
  while (temp != null) {
   System.out.format("%d ", temp.value);
   temp =;

// This function will find middle element in linkedlist
 public Node findMiddleNode(Node head)
 Node slowPointer, fastPointer; 
  slowPointer = fastPointer = head; 

  while(fastPointer !=null) { 
   fastPointer =; 
   if(fastPointer != null && != null) { 
    slowPointer =; 
    fastPointer =; 

  return slowPointer; 


 public static void main(String[] args) {
  LinkedList list = new LinkedList();
  // Creating a linked list
  Node head=new Node(5);
  list.addToTheLast(new Node(6));
  list.addToTheLast(new Node(7));
  list.addToTheLast(new Node(1));
  list.addToTheLast(new Node(2));
  // finding middle element
  Node middle= list.findMiddleNode(head);
  System.out.println("Middle node will be: "+ middle.value);


Logically linked list can be represented as:

Middle element is represented in red color.
Run above program, you will get following output:
5 6 7 1 2 
Middle node is: 7

Please go through java interview programs for more such programs.

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus


Java tutorial for beginners Copyright © 2012