Java Program for Insertion in Doubly Linked List

Insertion in a Doubly Linked List in Java

Lets have a look at the Java Program to Insert in a Doubly Linked List in Java. We will explore all the methods and positions to do insertion

In a doubly Linked List insertion can be done at the following positions –

  • At the beginning
  • At the end
  • After a given node
insertion in doubly linked list

Doubly Linked List Structure

Below is how node class is defined –

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

    Node(int x) // parameterized constructor
    {
        data = x;
        next = null; 
        prev = null;     
    }
}
Example for Doubly Linked List insertion in Java

For Insertion in the Beginning of the List

  • Change the address in the Head of the list to the address of the New Node.
  • The previous of the New Node will point to null since it is the first node of the list.
  • The next of the New Node will point to the previous initial node of the list.
Example for Doubly Linked List insertion in Java
public void insertBeginning(int data)
{
    // Creating newNode memory & assigning data value
    Node freshNode = new Node(data);

    freshNode.next = head;
    freshNode.prev = null;

    // if DLL had already >=1 nodes
    if(head !=null)
        head.prev = freshNode;

    // changing head to this
    head = freshNode;
}

For Insertion in the End​

  • Change the address in the Last Node of the list to the address of the New Node.
  • The previous of the New Node will point to the Previously Last Node of the list.
  • The next of the New Node will point to Null since the new node is the Last Node.
Example for Doubly Linked List insertion in Java
public void insertEnd(int data) {
    // Creating newNode memory & assigning data value
    Node freshNode = new Node(data);

    // assign data
    // since this will be the last node its next will be NULL
    freshNode.next = null;

    //if we are entering the first node
    if (head == null) {
        head = freshNode;
        freshNode.prev = null;
        return;
    }

    Node last = head;

    // traverse to the current last node
    while (last.next != null)
        last = last.next;

    // assign current last node's next to this new node
    // assign new node's previous to this last node
    last.next = freshNode;
    freshNode.prev = last;
    // new_node becomes the last node now

}

For Insertion at the Specific Position​

  • So, here the Node is being inserted at the Nth
    position in the list.
  • Change the address in the next of the (N-1)th  node of the list to the address of the New Node.
  • The previous of the New Node will point back to the (N-1)th  node of the list.
  • The next of the New Node will point to the (N+1)th  node of the list and vice versa.
Example for Doubly Linked List insertion in Java
public void insertAfterPosition(int n, int data) {
    int len = getLength(head);

    // if insertion position is 0 means entering at start
    if (n == 0) {
        insertBeginning(data);
        return;
    }
    // means inserting after last item
    if (n == len) {
        insertEnd(data);
        return;
    }

    // else insertion will happen somewhere in the middle


    if (n < 1 || len < n) 
        System.out.println("Invalid position");

    else {
        Node freshNode = new Node(data);

        // can remove null assignments also (constructor takes care of these)
        // added just for explanation purpose
        freshNode.next = null;
        freshNode.prev = null;

        // nthNode used to traverse the Linked List
        Node nthNode = head;

        // traverse till the nth node
        while (--n > 0) {
            nthNode = nthNode.next;
        }

        Node nextNode = nthNode.next; // (n+1)th node

        // assigning (n+1)th node's previous to this new node
        nextNode.prev = freshNode;

        // new_node's next assigned to (n+1)th node
        freshNode.next = nextNode;
        // new_node's previous assigned to nth node
        freshNode.prev = nthNode;

        // assign nth node's next to new_node
        nthNode.next = freshNode;
    }
}

// required for insertPosition() method
public int getLength(Node node) {
    int size = 0;
    // traverse to the last node each time incrementing the size
    while (node != null) {
        node = node.next;
        size++;
    }
    return size;
}

Complete Code for Insertion

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

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

Fun Fact

Quiz time

Doubly Linked List in Real Life can be seen implemented in back and forward buttons in a browser navigation. Also the undo and redo buttons are a great example of application of Doubly Linked List.