Java Program to Get the Middle Element of LinkedList
What is LinkedList in Java ?
A linked list is a linear data structure in which each element (called a node) stores a reference to the next element in the list. Linked lists are dynamic data structures, which means that their size can change during the execution of a program. In contrast, arrays have a fixed size, which means that you need to specify the size of the array when you create it and you cannot change it later.
Steps to Get the Middle Element of LinkedList :
1. Create two pointers: slow and fast, and initialize both to the head of the linked list.
2. Traverse the linked list using the fast pointer and move it two nodes at a time.
3.Move the slow pointer one node at a time.
4.When the fast pointer reaches the end of the linked list, the slow pointer will be pointing to the middle element.
Pseudo Code for the above algorithm :
public static ListNode getMiddleElement(ListNode head) { ListNode slow = head; ListNode fast = head; while (fast != null && fast.next != null) { fast = fast.next.next; slow = slow.next; } return slow; }
Explanation :
In this code, ListNode is a class representing a node in the linked list, with val and next properties representing the value of the node and a reference to the next node, respectively. head is the head node of the linked list. The code returns the middle node of the linked list. If the linked list has even number of nodes, it returns the second middle node.
Example :
Run
import java.util.LinkedList; public class Main { public static void main(String[] args) { // create a LinkedList with some elements LinkedList< Integer> list = new LinkedList<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); // find the middle element of the LinkedList int middleIndex = list.size() / 2; int currentIndex = 0; Integer middleElement = null; for (Integer element : list) { if (currentIndex == middleIndex) { middleElement = element; break; } currentIndex++; } // print the middle element System.out.println("Middle element: " + middleElement); } }
Output :
Middle element: 3
Explanation :
In this program, we create a LinkedList with some elements and then calculate the index of the middle element by dividing the size of the list by 2. We then use two pointers, one to keep track of the current index and another to store the middle element. We iterate through the list using a for-each loop and check if the current index is equal to the middle index. If it is, we store the current element in the middle element variable and break out of the loop. Finally, we print the middle element.
Example :
Run
import java.util.LinkedList; public class Main { public static void main(String[] args) { // create a LinkedList with some elements LinkedList< Integer> list = new LinkedList<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); // find the middle element of the LinkedList int middleIndex = list.size() / 2; Integer middleElement = list.get(middleIndex); // print the middle element System.out.println("Middle element: " + middleElement); } }
Output :
Middle element: 3
Explanation :
In this program, we create a LinkedList with some elements and then calculate the index of the middle element by dividing the size of the list by 2. We then use the get() method of the LinkedList class to retrieve the element at the middle index and store it in the middleElement variable. Finally, we print the middle element. The get() method provides a simple and efficient way to access elements in a LinkedList by index.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment