Data Structures · Visualization
Doubly LL: Delete
Doubly Linked List
Deleting a node in a doubly linked list is fast once you have a reference to it, because both its neighbors are directly reachable through prev and next — there is no need to search for the previous node like in a singly linked list. Time: O(1) if the node reference is known, O(n) if searching by value. Space: O(1).
Big-O: Insert/delete at either end O(1); extra memory for prev pointers.
Doubly LL: Delete
Doubly Linked ListDeleting a node in a doubly linked list is fast once you have a reference to it, because both its neighbors are directly reachable through prev and next — there is no need to search for the previous node like in a singly linked list. You simply connect the node's neighbors to each other, skipping over the deleted node.
How Delete Works
- Locate (or already have a reference to) the node to delete.
- If the node has a previous neighbor, set prev.next to the node's next.
- If the node has a next neighbor, set next.prev to the node's prev.
- Update head or tail if the deleted node was at either end.
Delete Worked Example
Input: Delete node 2 from null <- 1 <-> 2 <-> 3 -> null
- node2.prev (node1).next = node2.next (node3)
- node2.next (node3).prev = node2.prev (node1)
Output: null <- 1 <-> 3 -> null
Press ▶ Run to watch it step by step.
def delete_node(node):
# node is a pointer to the target
if node.prev:
node.prev.next = node.next
if node.next:
node.next.prev = node.prev
return node.prev or node.next
# O(1) once you hold the node
# example: delete 2 on [1, 2, 3]Behind the Scenes Execution
Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.
Delete Time & Space Complexity
When to Use Delete
- Removing an item from an LRU cache in constant time
- Deleting a specific entry from browser history
- Removing a node from a deque from either end
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.