Write a Java program to find the middle element of a singly linked list in one pass


The Java code provided demonstrates how to find the middle element of a linked list in a single pass.

  • The code defines a class named FindMiddle_Element with a main method, which serves as the entry point for the Java program.
  • Inside the main method, a LinkedList object named col_list is created, which represents the linked list. Note that LinkedList is a custom implementation of a linked list, defined in the same code.
  • LinkedList.Node is a static inner class within the LinkedList class, representing a node in the linked list. It has a data field to store the data of the node and a next field to store the reference to the next node in the list.
  • Nodes with data "Blue", "Pink", "Orange", "Yellow", and "White" are added to the col_list using the add method of LinkedList, which appends the nodes to the end of the list.
  • The code then finds the middle element of the linked list in a single pass. It initializes a current variable to the head of the list and keeps track of the length of the list using a len variable.
  • It also initializes a middle variable to the head of the list, which will initially point to the first node, and updates it to the next node at every even length of the list.
  • The code iterates through the linked list using a while loop and updates the middle variable at every even length of the list by moving it to the next node.
  • Finally, the code prints the length of the linked list and the middle element of the linked list using System.out.println statements.
  • The output of the program will be the length of the linked list and the middle element of the linked list.

Source Code

import java.util.*;
import test.LinkedList.Node;
public class FindMiddle_Element
{ 
    public static void main(String args[])
	{
      LinkedList col_list = new LinkedList();
      LinkedList.Node head = col_list.head();
      col_list.add( new LinkedList.Node("Blue"));
      col_list.add( new LinkedList.Node("Pink"));
      col_list.add( new LinkedList.Node("Orange"));
      col_list.add( new LinkedList.Node("Yellow"));
      col_list.add( new LinkedList.Node("White"));
 
      //finding middle element of LinkedList in single pass
      LinkedList.Node current = head;
      int len = 0;
      LinkedList.Node middle = head;
 
      while(current.next() != null){
          len++;
          if(len%2 ==0){
              middle = middle.next();
          }
          current = current.next();
      }
 
      if(len%2 == 1){
          middle = middle.next();
      }
 
      System.out.println("Length of LinkedList :  " + len);
      System.out.println("Middle Element of LinkedList : "+ middle);
 
    }
 
}
 
class LinkedList{
    private Node head;
    private Node tail;
 
    public LinkedList(){
        this.head = new Node("head");
        tail = head;
    }
 
    public Node head(){
        return head;
    }
 
    public void add(Node node){
        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;
        }
    }
}
 

Output

Length of LinkedList : 5
Middle Element of LinkedList : Orange

Example Programs