00:00

Deletion in Doubly Linked List

Hello, In this tutorial, we will see all about the delete operation in a doubly linked list. In the delete operation, we will delete an existing node from the doubly linked list from any position.

In a Doubly Linked List, we can delete a node at the following positions:

  1. Delete node at the beginning

  2. Delete node at the specific position

  3. Delete node at the end

Create a node class

Node.java
public class Node {
  public int data;      // Data stored in the node
  public Node prev;     // Reference to the next node in the list
  public Node next;     // Reference to the previous node in the list

  // Constructor to initialize a new node with data
  public Node(int new_data) {
    data = new_data;
    prev = null;
    next = null;
  }
}

1. Delete a node from the beginning of the doubly linked list

In the below given example we will see how to delete an existing node from the beginning or front the doubly linked list.

DeleteAtBeginningInDLL.java
// Java Program to delete a node from the beginning of a Doubly Linked List
public class DeleteAtBeginningInDLL {
  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 delete the first node (head) of a doubly linked list
  static Node deleteHead() {
    // Before deleting a node just make sure list should not be empty
    if (head == null) {
      System.out.println("List is empty");
      return null;
    }

    // Move head to the next node
    head = head.next;

    // If list contains only one node, head will be null
    // Otherwise, set previous node as null in the new head
    if (head != null) {
      head.prev = null;
    }
    return head;
  }

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

  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();

    deleteHead();

    System.out.println("\nAfter Deletion Head, Doubly Linked List data: ");
    printList();
  }
}
Output
Original doubly linked list:
10 20 30 40 50

After Deletion Head, Doubly Linked List data:
20 30 40 50

2. Delete a node at the specific position of the doubly linked list

In the below given example we will see how to delete an existing node from the any position of the doubly linked list.

DeleteAtSpecPositionInDLL.java
// Java Program to delete node at a specific position in Doubly Linked List
public class DeleteAtSpecPositionInDLL {
  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 delete a node at a specific position
  static Node delPos(Node head, int position) {

    // Before deleting a node just make sure list should not be empty
    if (head == null) {
      System.out.println("List is empty");
      return head;
    }

    Node curr = head;

    // Traverse to the node at the given position
    for (int i = 1; curr != null && i < position; ++i) {
      curr = curr.next;
    }

    // If the position is out of range
    if (curr == null)
      return head;

    // Update the previous node's next pointer
    if (curr.prev != null)
      curr.prev.next = curr.next;

    // Update the next node's prev pointer
    if (curr.next != null)
      curr.next.prev = curr.prev;

    // If the node to be deleted is the head node
    if (head == curr)
      head = curr.next;

    // Deallocate memory for the deleted node
    curr = null;
    return head;
  }

  // Method to print the doubly linked list
  static void printList(Node head) {
    Node curr = head;
    while (curr != null) {
      System.out.print(curr.data + " ");
      curr = curr.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);

    // Deleting node at 2nd Position
    head = delPos(head, 2);

    printList(head);
  }
}
Output
Original doubly linked list:
10 20 30 40 50
10 30 40 50

3. Delete a node from the end of the doubly linked list

In the below given example we will see how to delete an existing node from the end or last the doubly linked list.

DeleteAtEndInDLL.java
// Java Program to delete a node from the end/last of the Doubly Linked List
public class DeleteAtEndInDLL {
  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 delete the last node of the doubly linked list
  static Node deleteLastNode() {

    // Before deleting a node just make sure list should not be empty
    if (head == null) {
      System.out.println("List is empty");
      return null;
    }

    // Delete is not possible, if list has only one node
    if (head.next == null) {
      return null;
    }

    // Traverse to the last node
    Node curr = head;
    while (curr.next != null) {
      curr = curr.next;
    }

    // Update the previous node's next pointer
    if (curr.prev != null) {
      curr.prev.next = null;
    }

    // Return the updated head
    return head;
  }

  static void printList() {
    Node curr = head;
    while (curr != null) {
      System.out.print(curr.data + " ");
      curr = curr.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();

    deleteLastNode();

    System.out.println("\nAfter Deletion End, Doubly Linked List data: ");
    printList();
  }
}
Output
Original doubly linked list:
10 20 30 40 50

After Deletion End, Doubly Linked List data:
10 20 30 40