Data Structures · Visualization
Linked List: Delete
Linked List
Deleting a node removes it from the chain by skipping over it: the node right before it is re-pointed directly to the node right after it, so the deleted node is no longer reachable. Time: O(n) to find the node, O(1) to unlink once found. Space: O(1).
Big-O: Lookup O(n); insert/delete at head O(1).
Linked List: Delete
Linked ListDeleting a node removes it from the chain by skipping over it: the node right before it is re-pointed directly to the node right after it, so the deleted node is no longer reachable. If the node to delete is the head, the head pointer itself is simply moved forward.
How Delete Works
- If the node to delete is the head, move head to head.next and stop.
- Otherwise, traverse the list keeping track of the 'previous' node while looking for the target node.
- Once the target node is found, set previous.next to target.next, skipping over the target.
- The target node is now unreachable and can be discarded.
Delete Worked Example
Input: Delete value 2 from head -> 1 -> 2 -> 3 -> null
- Traverse: previous = 1, current = 2 (target found)
- previous.next = current.next (node 3)
Output: head -> 1 -> 3 -> null
Press ▶ Run to watch it step by step.
def delete_value(head, val):
if not head:
return None
if head.val == val:
return head.next # remove head
cur = head
while cur.next and cur.next.val != val:
cur = cur.next
if cur.next:
cur.next = cur.next.next # skip the node
return head
# 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 a completed task from a task queue
- Deleting a specific browser history entry
- Removing a player from a turn-based rotation
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.