The Java code provided demonstrates how to find the middle element of a linked list in a single pass.
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; } } }
Length of LinkedList : 5 Middle Element of LinkedList : Orange
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions