Insert a Node in the Middle of a Linked List in Java

Java Program for Insertion in the middle in a Linked List

Given below is the code for Insertion of a Node in the middle to a Linked List in Java. Program for insertion in the middle of linked list Java

Linked List Java

Insertion in the middle of linked list Java

Approaches followed

We will discuss the given approaches –

  1. Method 1: Extra Size variable to keep track of the current size of Linked List
  2. Method 2: Additional function to calculate the size of Linked List without extra variable
// Insertion in the middle of linked list Java
import java.lang.*; class LinkedList { Node head; int size = 0; // Node Class class Node{ int data; Node next; Node(int x) // parameterized constructor { data = x; next = null; } } public void insert(int data) { Node newNode = new Node(data); newNode.next = head; head = newNode; size++; } public void insertMiddle(int data) { Node newNode = new Node(data); // if the LL was empty if(head == null){ newNode.data = data; newNode.next = head; head = newNode; size++; return; } // otherwise Node temp = head; // find correct insertion position for middle int mid = (size % 2 == 0) ? (size/2) : (size+1)/2; // traverse to current mid position while(--mid > 0){ temp = temp.next; } newNode.next = temp.next; temp.next = newNode; size++; } public void display() { Node node = head; //as linked list will end when Node reaches Null while(node!=null) { System.out.print(node.data + " "); node = node.next; } System.out.println(); } public static void main(String args[]) { LinkedList ll = new LinkedList(); ll.insert(80); ll.insert(0); ll.display(); ll.insertMiddle(20); ll.display(); ll.insertMiddle(60); ll.display(); ll.insertMiddle(40); ll.display(); } }

Output

0 80 
0 20 80 
0 20 60 80 
0 20 40 60 80 
import java.lang.*;

class LinkedList {
    Node head;

    // Node Class
    class Node{
        int data;
        Node next;

        Node(int x) // parameterized constructor
        {
            data = x;
            next = null;
        }
    }
    public void insert(int data)
    {
        Node newNode = new Node(data);
        newNode.next = head;
        head = newNode;
    }
    public void insertMiddle(int data) {
        Node newNode = new Node(data);

        // if the LL was empty
        if(head == null){
            newNode.data = data;
            newNode.next = head;
            head = newNode;
            return;
        }

        // otherwise
        Node temp = head;

        int size = getCurrSize();

        // find correct insertion position for middle
        int mid = (size % 2 == 0) ? (size/2) : (size+1)/2;

        // traverse to current mid position
        while(--mid > 0){
            temp = temp.next;
        }

        newNode.next = temp.next;
        temp.next = newNode;
    }

    public void display()
    {
        Node node = head;
        //as linked list will end when Node reaches Null
        while(node!=null)
        {
            System.out.print(node.data + " ");
            node = node.next;
        }
        System.out.println();
    }
    public int getCurrSize(){
        int size = 0;
        Node node = head;

        // traverse to the last node each time incrementing the size
        while(node!=null){
            node = node.next;
            size++;
        }
        return size;
    }

    public static void main(String args[])
    {
        LinkedList ll = new LinkedList();

        ll.insert(80);
        ll.insert(0);

        ll.display();

        ll.insertMiddle(20);
        ll.display();

        ll.insertMiddle(60);
        ll.display();

        ll.insertMiddle(40);
        ll.display();
    }
}

Output

0 80 
0 20 80 
0 20 60 80 
0 20 40 60 80

Singly Linked List

  • Introduction to Linked List in Data Structure
    Click Here
  • Linked List in –
    C | C++ | Java
  • Singly Linked List in –
    C | C++ | Java
  • Insertion in singly Linked List –
    C | C++ | Java
  • Insertion at beginning in singly Linked List  –
    C | C++Java
  • Insertion at nth position in singly Linked List  –
    C | C++Java
  • Insertion at end in singly Linked List  –
    C | C++Java
  • Deletion in singly Linked List  –
    C | C++Java
  • Deletion from beginning in singly linked list :
    C | C++ | Java
  • Deletion from nth position in singly linked list :
    C | C++ | Java
  • Deletion from end in singly linked list :
    C | C++ | Java
  • Linked List Insertion and Deletion –
    C | C++Java
  • Reverse a linked list without changing links between nodes (Data reverse only) –
    C | C++Java
  • Reverse a linked list by changing links between nodes –
    C | C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Insertion in the middle Singly Linked List –
    C | C++Java
  • Insertion in a Sorted Linked List –
    C | C++Java
  • Delete alternate nodes of a Linked List –
    C | C++Java
  • Find middle of the linked list –
    C | C++Java
  • Reverse a linked list in groups of given size –
    C | C++Java
  • Find kth node from end of the linked list –
    C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list –
    C | C++Java
  • Check whether linked list is palindrome or not –
    C | C++Java
  • Fold a Linked List –
    C | C++Java
  • Insert at given Position –
    C | C++Java
  • Deletion at given Position –
    C | C++Java

Singly Linked List

  • Introduction to Linked List in Data Structure
  • Linked List in – C | C++ | Java
  • Singly Linked List in – C | C++ | Java
  • Insertion in singly Linked List – C | C++ | Java
    • Insertion at beginning in singly Linked List  – C | C++Java
    • Insertion at nth position in singly Linked List  – C | C++Java
    • Insertion at end in singly Linked List  – C | C++Java
  • Deletion in singly Linked List  – C | C++Java
    • Deletion from beginning in singly linked list : C | C++ | Java
    • Deletion from nth position in singly linked list : C | C++ | Java
    • Deletion from end in singly linked list : C | C++ | Java
  • Reverse a linked list without changing links between nodes (Data reverse only) – C | C++Java
  • Linked List Insertion and Deletion – C | C++Java
  • Reverse a linked list by changing links between nodes – C | C++Java
  • Linked List insertion in the middle – C | C++Java
  • Print reverse of a linked list without actually reversing – C |C++ | Java
  • Search an element in a linked list – C | C++Java
  • Insertion in a Sorted Linked List – C | C++Java
  • Delete alternate nodes of a Linked List – C | C++Java
  • Find middle of the linked list – C | C++Java
  • Reverse a linked list in groups of given size – C | C++Java
  • Find kth node from end of the linked list – C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list – C | C++Java
  • Check whether linked list is palindrome or not – C | C++Java
  • Fold a Linked List – C | C++Java
  • Insert at a given position – C | C++Java
  • Delete at a given position – C | C++Java