
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Get the middle element of LinkedList in a single iteration
The java.util.LinkedList class represents a linked list in Java, specifically a doubly-linked list, i.e., we can traverse through the list either from the beginning or from the end, whichever is closer to the specified index. We can create and perform various operations on a linked list using the methods provided by this class.
Retrieving the Middle Element of a LinkedList
In this article, we will understand how to find the middle element of a LinkedList using Java. We will use the following ways to do so:
Using a While Loop
We can find the middle element of a LinkedList using a while loop. We will use two pointers, one that moves one step at a time and another that moves two steps at a time. When the faster pointer reaches the end, the slower pointer will be at the middle.
Example
The following is an example to retrieve the middle element of a LinkedList using the while loop:
public class FindMiddleEl { class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } Node head; public void add(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; } else { Node current = head; while (current.next != null) { current = current.next; } current.next = newNode; } } public static void main(String[] args) { FindMiddleEl list = new FindMiddleEl(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); Node slowPointer = list.head; Node fastPointer = list.head; while (fastPointer != null && fastPointer.next != null) { slowPointer = slowPointer.next; fastPointer = fastPointer.next.next; } System.out.println("The middle element is: " + slowPointer.data); } }
Following is the output of the above code:
The middle element is: 3
Using size() Method
For the built-in LinkedList class, we can use the size() method to get the length of the list. We can then find the middle element by traversing the list up to the middle index (size/2).
Example
The following is an example to retrieve the middle element of a LinkedList using the size() method:
import java.util.LinkedList; public class FindMiddleEl { public static void main(String[] args) { LinkedList<Integer> list = new LinkedList<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); int middleIndex = list.size() / 2; int middleElement = list.get(middleIndex); System.out.println("The middle element is: " + middleElement); } }
Following is the output of the above code:
The middle element is: 3
Using Stream API
In the Java Stream API, we can convert the LinkedList to a stream and then use the skip() method to skip the first half of the elements. The findFirst() method will give us the middle element.
Example
The following is an example to retrieve the middle element of a LinkedList using the Streams API:
import java.util.LinkedList; import java.util.stream.IntStream; public class FindMiddleEl { public static void main(String[] args) { LinkedList<Integer> list = new LinkedList<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); int middleIndex = list.size() / 2; int middleElement = IntStream.range(0, list.size()) .filter(i -> i == middleIndex) .mapToObj(list::get) .findFirst() .orElseThrow(); System.out.println("The middle element is: " + middleElement); } }
Following is the output of the above code:
The middle element is: 3