Delete Operation in Doubly Linked List
After deleting the node at index/position = 2 (value 30).
Deleting from a doubly linked list involves removing a specific node and adjusting both the next pointer of the previous node and the prev pointer of the next node.
This operation can occur at the beginning, in the middle, or at the end of the list. Doubly linked lists allow efficient deletion from any position as each node has pointers in both directions.
Visual Example
Suppose we have a doubly linked list:
We want to delete the node at index 2 (value 30). After deletion, the list becomes: 10 ⇄ 20 ⇄ 40 ⇄ null
Step-by-Step Deletion at Index 2 (Doubly Linked List)
- Start with list
10 ⇄ 20 ⇄ 30 ⇄ 40 ⇄ null.
- Traverse to the node at index
2 (value 30).
- Update the previous node's
next pointer:
30.prev.next = 30.next (i.e., 20 → 40).
- Update the next node's
prev pointer:
30.next.prev = 30.prev (i.e., 40 ← 20).
- The node with value
30 is now disconnected and will be garbage collected.
Complete Pseudocode: Delete Node in Doubly Linked List
This pseudocode shows how to delete a node at a specific index in a doubly linked list, handling both forward and backward links.
function deleteAt(head, index):
if index == 0:
head = head.next
if head is not null:
head.prev = null
return head
curr = head
for i in range(index):
curr = curr.next
if curr is not null:
if curr.prev is not null:
curr.prev.next = curr.next
if curr.next is not null:
curr.next.prev = curr.prev
return head
Let’s walk through each part of the code to understand how it works in a doubly linked list context.
function deleteAt(head, index):
Explanation: This function deletes the node at the specified index from a doubly linked list.
if index == 0:
head = head.next
if head is not null:
head.prev = null
return head
Explanation: If the node to delete is the head, we move the head pointer forward and detach the new head from the old one by setting head.prev to null.
curr = head
for i in range(index):
curr = curr.next
Explanation: We move the curr pointer to the node at the target index.
if curr is not null:
if curr.prev is not null:
curr.prev.next = curr.next
if curr.next is not null:
curr.next.prev = curr.prev
Explanation: If the node exists, we unlink it in both directions: its previous node skips forward, and its next node skips back, effectively removing curr from the list.
return head
Explanation: The function returns the updated head of the doubly linked list.

About the authorMallikarjuna Mallisetty
General programming
Mallikarjuna shares practical programming tutorials and foundational concepts designed to help developers learn by building and experimenting.
View LinkedIn profile ↗