
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
Get Middle Element of LinkedList in a Single Iteration
In this article, we will understand how to get the middle element of linkedList in a single iteration. The java.util.LinkedList class operations perform we can expect for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.
Below is a demonstration of the same −
Suppose our input is −
Input linked list: 100 200 330
The desired output would be −
The middle element of the list is: 200
Algorithm
Step 1 - START Step 2 - Declare a LinkedList namely input_list. Declare five node objects namely head, first_node, second_node, pointer_1, pointer_2. Step 3 - Define the values. Step 4 - Using a while loop, iterate over the linked list, get the middle element by traversing the list using pointer_1 and pointer_2 until pointer_1.next is not null. Step 5 - Display the pointer_2 value as result. Step 6 - Stop
Example 1
Here, we bind all the operations together under the ‘main’ function.
public class LinkedList { Node head; static class Node { int value; Node next; Node(int d) { value = d; next = null; } } public static void main(String[] args) { LinkedList input_list = new LinkedList(); input_list.head = new Node(100); Node second_node = new Node(200); Node third_node = new Node(330); input_list.head.next = second_node; second_node.next = third_node; Node current_node = input_list.head; System.out.print("The linked list is defined as: " ); while (current_node != null) { System.out.print(current_node.value + " "); current_node = current_node.next; } Node pointer_1 = input_list.head; Node pointer_2 = input_list.head; while (pointer_1.next != null) { pointer_1 = pointer_1.next; if(pointer_1.next !=null) { pointer_1 = pointer_1.next; pointer_2 = pointer_2.next; } } System.out.println("\nThe middle element of the list is: " + pointer_2.value); } }
Output
The linked list is defined as: 100 200 330 The middle element of the list is: 200
Example 2
Here, we encapsulate the operations into functions exhibiting object oriented programming.
public class LinkedList { Node head; static class Node { int value; Node next; Node(int d) { value = d; next = null; } } static void get_middle_item(LinkedList input_list){ Node pointer_1 = input_list.head; Node pointer_2 = input_list.head; while (pointer_1.next != null) { pointer_1 = pointer_1.next; if(pointer_1.next !=null) { pointer_1 = pointer_1.next; pointer_2 = pointer_2.next; } } System.out.println("\nThe middle element of the list is: " + pointer_2.value); } public static void main(String[] args) { LinkedList input_list = new LinkedList(); input_list.head = new Node(100); Node second_node = new Node(200); Node third_node = new Node(330); input_list.head.next = second_node; second_node.next = third_node; Node current_node = input_list.head; System.out.print("The linked list is defined as: " ); while (current_node != null) { System.out.print(current_node.value + " "); current_node = current_node.next; } get_middle_item(input_list); } }
Output
The linked list is defined as: 100 200 330 The middle element of the list is: 200
Advertisements