- 1Singly Linked List Introduction
- 2Singly Linked List – Create Operation
- 3Singly Linked List – Insert Operation
- 4Singly Linked List – Delete Operation
- 5Singly Linked List – Traversal Operation
- 6Singly Linked List – Search Operation
- 7Singly Linked List – Get Length Operation
- 8Singly Linked List – Reverse Operation
- 9Doubly Linked List Introduction
- 10Doubly Linked List – Create Operation
- 11Doubly Linked List – Insert Operation
- 12Doubly Linked List – Delete Operation
- 13Doubly Linked List – Traversal Operation
- 14Doubly Linked List – Search Operation
- 15Doubly Linked List – Get Length Operation
- 16Doubly Linked List – Reverse Operation
Doubly Linked List – Delete Operation
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
(value30
). - 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.
head.prev to null.", "If the index is greater than 0, initialize a pointercurr
tohead
.", "Loopindex
times to movecurr
to the target node.", "Check ifcurr
is not null, which ensures a node exists at that index.", "Ifcurr.prev
exists, setcurr.prev.next
tocurr.next
.", "Ifcurr.next
exists, setcurr.next.prev
tocurr.prev
.", "This detaches thecurr
node from the list in both directions.", "Returnhead
of the updated list." ]'>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.
Initial List | Delete Operation | Resulting List |
---|---|---|
10 ⇄ 20 ⇄ 30 ⇄ 40 | Delete from Beginning | 20 ⇄ 30 ⇄ 40 |
10 ⇄ 20 ⇄ 30 ⇄ 40 | Delete from End | 10 ⇄ 20 ⇄ 30 |
10 ⇄ 20 ⇄ 30 ⇄ 40 | Delete node with value 20 | 10 ⇄ 30 ⇄ 40 |
10 | Delete from Beginning | [Empty List] |
[Empty List] | Delete from End | [Empty List] |
Time Complexity of Doubly Linked List Delete Operation
Deletion Case | Time Complexity | Explanation |
---|---|---|
Delete at Beginning | O(1) | We update the head pointer to the next node and set its prev to null . No traversal is required. |
Delete at Middle | O(n) | We traverse to the node to delete, then update its prev.next and next.prev pointers. Finding the node takes linear time. |
Delete at End | O(n) | We traverse to the last node, then update the tail or prev.next pointer and set the last node's prev to null . |
Delete by Value | O(n) | We scan the list to find the first node with the target value. Once found, we update both adjacent pointers to unlink the node. |
Space Complexity of Doubly Linked List Delete Operation
Deletion Case | Space Complexity | Explanation |
---|---|---|
Delete at Beginning | O(1) | No extra memory is needed. Only the head pointer and the second node’s prev are updated. |
Delete at Middle | O(1) | Only local pointer adjustments are made. The prev and next pointers of adjacent nodes are reassigned. |
Delete at End | O(1) | Even if traversal is required, no extra memory is used. The prev node's next is set to null . |
Delete by Value | O(1) | Once the node is located, deletion involves constant-space pointer changes. |
Key Takeaways
- Efficient Bidirectional Deletion: Doubly linked lists allow deletion in constant time
O(1)
once the target node is located, thanks to bothprev
andnext
pointers. - Pointer Updates: When deleting a node, always update both the previous node’s
next
and the next node’sprev
to maintain list integrity. - Deletion at Head: Update the
head
pointer and ensure the new head'sprev
is set tonull
. - Deletion at Tail: Set the previous node’s
next
tonull
and update thetail
pointer if maintained. - No Extra Space: All deletions are done in-place with no additional memory requirements, giving a space complexity of
O(1)
. - Safe Deletion: Always check for null references before accessing or updating
prev
andnext
pointers to avoid runtime errors.
Comments
Loading comments...