Deleting a node from a doubly linked list means joining its two neighbours to each other: the previous node's next skips over it and the next node's prev skips back over it. Because every node already knows its predecessor, you can stop on the node to delete rather than on the node before it.
This tutorial covers deletion from three places:
Delete node at the beginning
Delete node at the specific position
Delete node at the end
Create a node class
public class Node {
public int data; // Data stored in the node
public Node prev; // Reference to the previous node in the list
public Node next; // Reference to the next 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
Move head to the second node and clear that node's prev, because it is now the first node. If the list had only one node, head simply becomes null. This is O(1).
// 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() {
// Nothing to delete in an empty list
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();
}
}
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
Walk a pointer curr to the node at the given position (counting from 1). Then link its neighbours together: curr.prev.next = curr.next and curr.next.prev = curr.prev, skipping whichever neighbour does not exist. If curr was the head, the head moves to curr.next. Reaching the position is O(n).
// 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) {
// Nothing to delete in an empty list
if (head == null) {
System.out.println("List is empty");
return head;
}
// Positions start at 1
if (position < 1) {
System.out.println("Invalid position");
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;
// The removed node is no longer referenced by the list,
// so the garbage collector will reclaim it
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);
}
}
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
Walk to the last node and set its predecessor's next to null. If the list has a single node, the list becomes empty. Without a tail reference the walk makes this O(n); with one it is O(1), because the new last node is simply tail.prev.
// 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() {
// Nothing to delete in an empty list
if (head == null) {
System.out.println("List is empty");
return null;
}
// If the list has only one node, the list becomes empty
if (head.next == null) {
head = 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();
}
}
Original doubly linked list: 10 20 30 40 50 After Deletion End, Doubly Linked List data: 10 20 30 40