00:00

Insertion in Doubly Linked List

Last updated:

Inserting into a doubly linked list works like inserting into a singly linked list, with one extra duty: besides the next links, the prev links of the new node and of its neighbours must also be set, so that the list reads correctly in both directions.

This tutorial covers three insert positions:

  1. At the beginning of the list.

  2. At the end of the list.

  3. At the specific position of the list.

1. Insert a node at the beginning of the list.

The new node points forward to the old head, and the old head (if there is one) points back to the new node. The new node then becomes the head. Only a constant number of links change, so this is O(1).

InsertAtBeginningInDLL.java
// Java Program to insert a node at the beginning of a doubly linked list
public class InsertAtBeginningInDLL {
  static class Node {
    int data;
    Node prev;
    Node next;

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

  static Node head;

  // Method to insert a node in doubly linked list
  static void insertNode(int data) {
    Node newNode = new Node(data);
    Node last = head;
    if (head == null) {
      head = newNode;
      return;
    }
    while (last.next != null) {
      last = last.next;
    }
    last.next = newNode;
    newNode.prev = last;
  }

  // Method to insert a node at the beginning of the list
  static Node insertAtBeginning(Node head, int data) {

    Node new_node = new Node(data);
    new_node.next = head;

    // Set previous of head as new node
    if (head != null) {
      head.prev = new_node;
    }

    return new_node;
  }

  // Method to print the doubly linked list
  public static void printList(Node head) {
    Node node = head;
    while (node != null) {
      System.out.print(node.data + " ");
      node = node.next;
    }
    System.out.println();
  }

  public static void main(String[] args) {
    // Create a doubly linked list
    insertNode(10);
    insertNode(20);
    insertNode(30);
    insertNode(40);
    insertNode(50);

    System.out.println("Original doubly linked list:");
    printList(head);

    // Insert a new node at the front of the list
    head = insertAtBeginning(head, 90);

    // Print the updated list
    System.out.print("List, After inserting a node at the beginning:\n");
    printList(head);
  }
}
Output
Original doubly linked list:
10 20 30 40 50
List, After inserting a node at the beginning:
90 10 20 30 40 50

2. Insert a node at the end of the doubly linked list.

Walk to the last node, set its next to the new node and the new node's prev to it. If the list is empty, the new node becomes the head. The walk makes this O(n); with a tail reference it would be O(1).

InsertAtEndInDLL.java
// Java Program to insert a node at the end of doubly linked list
public class InsertAtEndInDLL {
  static class Node {
    int data;
    Node prev;
    Node next;

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

  static Node head;

  // Method to insert a node in doubly linked list
  static void insertNode(int data) {
    Node newNode = new Node(data);
    Node last = head;
    if (head == null) {
      head = newNode;
      return;
    }
    while (last.next != null) {
      last = last.next;
    }
    last.next = newNode;
    newNode.prev = last;
  }

  // Method to insert a new node at the end of the DLL
  static Node insertNodeAtEnd(Node head, int newData) {

    Node newNode = new Node(newData);
    if (head == null) {
      head = newNode;
    } else {
      Node node = head;
      while (node.next != null) {
        node = node.next;
      }

      // Set the next of last node to the new node
      node.next = newNode;

      // Set the prev of new node to the last node
      newNode.prev = node;
    }

    return head;
  }

  // Method to print the doubly linked list
  public static void printList(Node head) {
    Node node = head;
    while (node != null) {
      System.out.print(node.data + " ");
      node = node.next;
    }
    System.out.println();
  }

  public static void main(String[] args) {
    // Create a doubly linked list
    insertNode(10);
    insertNode(20);
    insertNode(30);
    insertNode(40);
    insertNode(50);

    System.out.println("Original doubly linked list:");
    printList(head);

    // Insert a new node with data 60 at the end
    head = insertNodeAtEnd(head, 60);

    System.out.println("After inserting, doubly linked list:");
    printList(head);
  }
}
Output
Original doubly linked list:
10 20 30 40 50
After inserting, doubly linked list:
10 20 30 40 50 60

3. Insert a node at a specific position of the list

Positions are counted from 1. Move a pointer curr to the node at position − 1, then link the new node between curr and curr.next: set the new node's prev and next, then curr.next, and finally the prev of the following node if it exists. Position 1 is handled like insertion at the beginning, positions below 1 are rejected, and a position past the end is reported as out of bounds.

InsertAtSpecPositionInDLL.java
// Java Program to insert a node at the specific position in DLL
public class InsertAtSpecPositionInDLL {
  static class Node {
    int data;
    Node prev;
    Node next;

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

  static Node head;

  // Method to insert a node in a doubly linked list
  static void insertNode(int data) {
    Node newNode = new Node(data);
    Node last = head;
    if (head == null) {
      head = newNode;
      return;
    }
    while (last.next != null) {
      last = last.next;
    }
    last.next = newNode;
    newNode.prev = last;
  }

  // Method to insert a node at the specific position in a doubly linked list
  static Node insertNodeAtPosition(Node head, int position, int data) {
    // Positions start at 1
    if (position < 1) {
      System.out.println("Error: Position must be 1 or greater.");
      return head;
    }

    Node new_node = new Node(data);

    // Insertion at the beginning
    if (position == 1) {
      new_node.next = head;

      if (head != null) {
        head.prev = new_node;
      }

      head = new_node;
      return head;
    }

    Node curr = head;

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

    // If the position is out of bounds
    if (curr == null) {
      System.out.println("Error: Given position is out of bounds.");
      return head;
    }

    new_node.prev = curr;
    new_node.next = curr.next;
    curr.next = new_node;

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

    return head;
  }

  // Method to print the doubly linked list
  public static void printList(Node head) {
    Node node = head;
    while (node != null) {
      System.out.print(node.data + " ");
      node = node.next;
    }
    System.out.println();
  }

  public static void main(String[] args) {
    // Create a doubly linked list
    insertNode(10);
    insertNode(20);
    insertNode(30);
    insertNode(40);
    insertNode(50);

    System.out.println("Original doubly linked list:");
    printList(head);

    // Insert new node with data 25 at position 3
    int data = 25;
    int position = 3;
    head = insertNodeAtPosition(head, position, data);

    System.out.println("After inserting, doubly linked list:");
    printList(head);
  }
}
Output
Original doubly linked list:
10 20 30 40 50
After inserting, doubly linked list:
10 20 25 30 40 50