Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regarding finding the middle element of linked list

Tags:

java

I am following the below approach to calculate the middle element from the linked list , but I want is there any built in method or any other approach which can also find the same easily , the approach that I am following is shown bellow..

import test.LinkedList.Node;
public class LinkedListTest {


    public static void main(String args[]) {
        //creating LinkedList with 5 elements including head
      LinkedList linkedList = new LinkedList();
      LinkedList.Node head = linkedList.head();
      linkedList.add( new LinkedList.Node("1"));
      linkedList.add( new LinkedList.Node("2"));
      linkedList.add( new LinkedList.Node("3"));
      linkedList.add( new LinkedList.Node("4"));

      //finding middle element of LinkedList in single pass
      LinkedList.Node current = head;
      int length = 0;
      LinkedList.Node middle = head;

      while(current.next() != null){
          length++;
          if(length%2 ==0){
              middle = middle.next();
          }
          current = current.next();
      }

      if(length%2 == 1){
          middle = middle.next();
      }

      System.out.println("length of LinkedList: " + length);
      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: 4
middle element of LinkedList : 2
like image 375
user2080568 Avatar asked Feb 20 '13 08:02

user2080568


People also ask

What is the middle node of linked list?

The node at which the pointer is pointing is the middle node of the linked list.

What is middle element of even linked list?

If there are even number of elements in a linked list such as 1 -> 2 -> 3 -> 4 -> 5 -> 6, then the middle element is 3 and 4.

Which of the following algorithm is the optimal way to find the middle element of the linked list?

Answer: B) Explanation: Fast and Slow pointer method is the most efficient way to find the middle element of the linked list.


2 Answers

The basic algorithm would be

  • Take two pointers

  • Make both pointing to first node

  • Increment first with two nodes and second with one, at a time.

  • Loop until the 1st loop reaches the end. At this point, the 2nd will be at the middle.

Example:-

while ( p2.next != null ) {
    p2 = p2.next;
    if (p2.next != null) {
        p2 = p2.next;
        p1 = p1.next;
    }
}

It will definitely work in odd case, for even case you need to check one more condition if first point is allowed to move next but not next to next then both pointers will be at middle you need to decide which to take as middle.

like image 146
Rahul Avatar answered Oct 02 '22 01:10

Rahul


I would recommend using the Java built in

LinkedList<Object e>

It gives you all the functionality you need like getting the length: list.size(), and the middle object:

list.get((list.size())/2);
like image 45
Michael A Avatar answered Oct 02 '22 01:10

Michael A