JAVA Program for Insertion at the Nth Position of a Doubly Linked List

Insertion at the Nth Position of a Doubly Linked List in Java

Insertion at the Nth Position of a Doubly Linked List in Java is an important operation that allows you to insert a new node at a specific position in an already existing doubly linked list. This operation is widely used in real world applications where data needs to be inserted dynamically at a specific location without breaking the structure of the list.

Insert a node at nth position in doubly linked list

Java Program for Insertion at the Nth Position of a Doubly Linked List

What Does Insertion at the Nth Position Mean?

In a doubly linked list, each node contains:

  • data
  • prev pointer (points to previous node)
  • next pointer (points to next node)

Insertion at the Nth position means adding a new node at a specific index, while keeping the rest of the list connected properly.

Example:

Original list:

10 ⇆ 20 ⇆ 40 ⇆ 50

Insert 30 at position 3

Result:

10 ⇆ 20 ⇆ 30 ⇆ 40 ⇆ 50

The new node is placed at the Nth spot and all links are updated properly.

Learning Insertion at the Nth Position of a Doubly Linked List in Java helps you understand:

  • Precise node positioning
  • Bidirectional pointer manipulation
  • Handling edge cases (beginning, middle, end)
  • Data organization in real world applications
Insertion at Nth position in a doubly linked list

Methods for Insertion at the Nth Position of a Doubly Linked List in Java

Here for Insertion at the Nth Position of a Doubly Linked List in Java, following methods can be applicable:

Method for Insertion at the Nth Position of a Doubly Linked List in Java

Method 1: Iterative Traversal Approach

Algorithm:

  1. Create a new node with the given value.
  2. If position == 1:
    • Set newNode.next = head
    • If head is not null, set head.prev = newNode
    • Update head = newNode
    • Return head
  3. Traverse the list using pointer temp till (position – 1)th node.
  4. If temp == null, insertion is not possible.
  5. Set:
    • newNode.next = temp.next
    • newNode.prev = temp
  6. If temp.next != null, set temp.next.prev = newNode

  7. Set temp.next = newNode

  8. Return head.

Java Code:

Run
class InsertAtNthDoubly {

    static class Node {
        int data;
        Node prev;
        Node next;

        Node(int data) {
            this.data = data;
        }
    }

    public static Node insertAtPosition(Node head, int data, int position) {
        Node newNode = new Node(data);

        // Insert at beginning
        if (position == 1) {
            newNode.next = head;
            if (head != null) {
                head.prev = newNode;
            }
            return newNode;
        }

        Node temp = head;

        for (int i = 1; i < position - 1 && temp != null; i++) {
            temp = temp.next;
        }

        if (temp == null) {
            return head;
        }

        newNode.next = temp.next;
        newNode.prev = temp;

        if (temp.next != null) {
            temp.next.prev = newNode;
        }

        temp.next = newNode;

        return head;
    }

    public static void printList(Node head) {
        Node temp = head;
        while (temp != null) {
            System.out.print(temp.data + " ");
            temp = temp.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Node head = new Node(10);
        Node second = new Node(20);
        Node third = new Node(40);

        head.next = second;
        second.prev = head;
        second.next = third;
        third.prev = second;

        System.out.println("Before insertion:");
        printList(head);

        head = insertAtPosition(head, 30, 3);

        System.out.println("After insertion at 3rd position:");
        printList(head);
    }
}

Input:

10 20 40
Insert: 30 at position 3

Output:

10 20 30 40

Method for Insertion at the Nth Position of a Doubly Linked List in Java

Method 2: Using Doubly Linked List Class

Algorithm:

  1. Maintain a head in class
  2. Traverse to (n-1)th node
  3. Insert new node between prev and next nodes
  4. If position is 1, shift head
  5. If end is reached, insert as tail

Java Code:

Run
class DoublyLinkedListNthInsert {

    static class Node {
        int data;
        Node prev, next;

        Node(int data) {
            this.data = data;
        }
    }

    Node head;

    public void insertAtPosition(int data, int position) {
        Node newNode = new Node(data);

        if (position == 1) {
            if (head != null) {
                head.prev = newNode;
            }
            newNode.next = head;
            head = newNode;
            return;
        }

        Node temp = head;
        for (int i = 1; i < position - 1 && temp != null; i++) {
            temp = temp.next;
        }

        if (temp == null) return;

        newNode.next = temp.next;
        newNode.prev = temp;

        if (temp.next != null) {
            temp.next.prev = newNode;
        }

        temp.next = newNode;
    }

    public void printList() {
        Node temp = head;
        while (temp != null) {
            System.out.print(temp.data + " ");
            temp = temp.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        DoublyLinkedListNthInsert list = new DoublyLinkedListNthInsert();

        list.insertAtPosition(10, 1);
        list.insertAtPosition(20, 2);
        list.insertAtPosition(40, 3);

        System.out.println("Before:");
        list.printList();

        list.insertAtPosition(30, 3);

        System.out.println("After insertion at position 3:");
        list.printList();
    }
}

Input:

10 20 40
Insert: 30 at position 3

Output:

10 20 30 40

Conclusion:

Insertion at the Nth position in a doubly linked list demonstrates the power of bidirectional linking in data structures.

  • By maintaining both prev and next pointers, the structure allows smooth and controlled insertion between any two nodes.
  • While traversal is still required to reach the correct position, the insertion itself is handled efficiently with constant pointer manipulation.
  • This operation highlights the importance of reference management in memory based data structures and builds a strong foundation for more advanced topics such as deletion at the Nth position, reversing a doubly linked list, and implementing circular list structures.

Mastering Insertion at the Nth Position of a Doubly Linked List in Java is essential for developing high performance data handling applications.

FAQ's for Insertion at the Nth Position of a Doubly Linked List

Answer:

It is the process of inserting a new node at a specific position in a doubly linked list by updating both previous and next pointers correctly.

Answer:

By traversing up to the (n-1)th node and linking the new node between the existing nodes using prev and next references.

Answer:

Yes, it is a very popular interview question in Java and DSA based technical rounds worldwide.

Answer:

It is used in text editors, browser history, playlist management, and dynamic sequence applications.

Answer:

Because it maintains backward links, making the pointer adjustment more reliable and efficient.

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

Doubly Linked List

  • Introduction to Doubly Linked list in Data Structure
    Click Here
  • Doubly Linked List in –
    C | C++ | Java
  • Insertion in doubly linked list –
    C | C++ | Java
  • Insertion at beginning in doubly linked list –
    C | C++ | Java
  • Insertion at end in doubly linked list –
    C | C++ | Java
  • Insertion at nth node in doubly linked list –
    C | C++ | Java
  • Deletion in doubly linked list  –
    C | C++ | Java
  • Deletion from beginning in doubly linked list :
  • Deletion from nth in doubly linked list :
    C | C++ | Java
  • Deletion from end in doubly linked list :
    C | C++ | Java
  • Insertion and Deletion in a  doubly linked list :
    C | C++ | Java
  • Insertion in the middle in a  doubly linked list :
    C | C++ | Java

Doubly Linked List