Dark Mode On/Off
PUBLISHED ON: APRIL 20, 2021

# Java Program to Find the Middle Element in a Linked List

In this tutorial, we will see how to find the middle element of a linked list in java. LinkedList is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. Each element is known as a node. Due to the dynamicity and ease of insertions and deletions, they are preferred over the arrays. But before moving further, if you are not familiar with the concept of the linked list in java, then do check the article on Linked List in Java.

Input: Enter the Linked List elements: 6 7 8 4 5

Output: The middle element of the Linked List is: 8

This can be done by using the following methods:

Approach 1: Using the get() method

Approach 2: Finding the Middle Element in a Single Pass

Let us look at each of these approaches for a better understanding.

## Program 1: Java Program to Find the Middle Element in a Linked List

In this program, we will see how to find the middle element in a linked list.

### Algorithm:

1. Start
2. Create a linked list of integer types using the linked list class.
4. First, find the middle index of the linked list by using the length by 2 formula.
5. Use the get(index) method to find the element.
6. Print the middle element.
7. Stop.

Let us look at the below example for a better understanding of the above algorithm.

``````//Java Program to Find the Middle Element in a Linked List
public class Main
{
public static void main(String[] args)
{
// access middle element
int middle = ll.get(ll.size()/2);
System.out.println("Middle Element: " + middle);
}
}``````

LinkedList: [3, 2, 5, 6, 8, 13, 18]
Middle Element: 6

## Program 2: Java Program to Find the Middle Element in a Linked List

In this program, we will see how to find the middle element in a linked list.

### Algorithm:

1. Start
2. Create a linked list of string types using the linked list data structure.
4. First, find the length of the linked list.
5. Use two pointers for the same.
6. The first pointer will increment on each iteration whereas the second pointer will be incremented every second iteration.
7. When the first pointer will point to the end of a linked list, the second pointer will be pointing to the middle element of the linked list.
8. Print the middle element.
9. Stop.

Let us look at the below example for a better understanding of the above algorithm.

``````public class LinkedListTest
{
public static void main(String args[])
{
//finding middle element of LinkedList in single pass
int length = 0;
while(current.next() != null){
length++;
if(length%2 ==0){
middle = middle.next();
}
current = current.next();
}
if(length%2 == 1){
middle = middle.next();
}
System.out.println("The middle element of the Linked List: "+ middle);
}
}
private Node tail;
}
}
tail.next = node;
tail = node;
}
public static class Node{
private Node next;
private String data;
public Node(String data){
this.data = data;
}
public String data() {
return data;
}
public void setData(String data) {
this.data = data;
}
public Node next() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public String toString(){
return this.data;
}
}
}``````

The middle element of the Linked List: Orange

Want to learn coding?
Try our new interactive courses.
Over 20,000+ students enrolled.