English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this example, we will learn how to get the middle element of a linked list in one iteration in Java.
To understand this example, make sure you first visit the following tutorials:
class LinkedList { //Create an object of the Node class //Represents the head of the list Node head; //Static inner class static class Node { int value; //Connect each node to the next node Node next; Node(int d) { value = d; next = null; } } public static void main(String[] args) { //Create a LinkedList object LinkedList linkedList = new LinkedList(); //Assign values to each linked list node linkedList.head = new Node(1); Node second = new Node(2); Node third = new Node(3); //Connect each node in the linked list to the next node linkedList.head.next = second; second.next = third; //Print the linked list Node pointer = linkedList.head; System.out.print("LinkedList: ") while (pointer != null) { System.out.print(pointer.value + "); pointer = pointer.next; } // Find the middle element Node ptr1 = linkedList.head; Node ptr2 = linkedList.head; while (ptr1.next != null) { //will point to the middle element1increase2,and ptr2increase1 //If ptr1will point to the last element //ptr2will point to the middle element ptr1 = ptr1.next; if(ptr1.next != null) { ptr1 = ptr1.next; ptr2 = ptr2.next; } } System.out.println("\nMiddle element: " + ptr2.value); } }
Output Result
LinkedList: 1 2 3 Middle element: 2
In the above example, we have implemented the linked list data structure in Java. Then, we find the middle element of the linked list in a loop. Note the code,
while (ptr1.next != null) { //will point to the middle element1increase2,and ptr2increase1 //If ptr1will point to the last element //ptr2will point to the middle element ptr1 = ptr1.next; if(ptr1.next != null) { ptr1 = ptr1.next; ptr2 = ptr2.next; } }
Here, we have two variables ptr1and ptr2. We use these variables to traverse the linked list.
In each iteration, ptr1will access two nodes, while ptr2will access a single node in the linked list.
Now, when ptr1When reaching the end of the linked list, ptr2It is located in the middle. Therefore, we can obtain the middle position of the linked list in a single iteration.
import java.util.LinkedList; class Main { public static void main(String[] args){ //Create a linked list using the LinkedList class LinkedList<String> animals = new LinkedList<>(); //Add elements to LinkedList animals.add("Dog"); animals.addFirst("Cat"); animals.addLast("Horse"); System.out.println("LinkedList: ", + animals); //Access the middle element String middle = animals.get(animals.size())/2); System.out.println("Middle Element: ", + middle); } }
Output Result
LinkedList: [Cat, Dog, Horse] Middle Element: Dog
In the above example, we used the LinkedList class to implement the linked list data structure. Note the expression
animals.get(animals.size())/2)
size()/ 2 - Return the position of the middle element
get() - Return the element located in the middle position