Data Structures · Visualization
Circular Linked List: Delete
Circular Linked List
Deleting a node from a circular linked list requires re-linking its neighbor's next pointer around it, with special handling when deleting the head, the tail, or the only remaining node. Time: O(n) to find the node (O(1) if a direct reference is already held). Space: O(1).
Big-O: Same as a singly linked list — O(1) insert/delete at the ends (with a tail pointer), O(n) search/delete by value.
Circular Linked List: Delete
Circular Linked ListDeleting a node from a circular linked list requires re-linking its neighbor's next pointer around it, with special handling when deleting the head, the tail, or the only remaining node.
How Delete Works
- Locate the node to delete and the node just before it (predecessor) by traversing the ring.
- If it's the only node, set head and tail to null.
- If deleting the head, move head to head.next, then point tail.next to the new head.
- Otherwise, point predecessor.next to the deleted node's next, skipping over it.
- If the deleted node was tail, update tail to the predecessor.
Delete Worked Example
Input: Delete 20 from ring 10 → 20 → 30 → (10)
- Traverse: predecessor = 10, target = 20
- predecessor.next = target.next (30)
- 10.next is now 30
- 20 is unlinked and garbage collected
Output: New ring: 10 → 30 → (back to 10)
Press ▶ Run to watch it step by step.
def delete_node(head, value):
if head is None or head.next is head: # 0 or 1 node
return None if head is None or head.val == value else head
cur = head
prev = None
while True:
if cur.val == value:
if cur is head:
tail = head
while tail.next is not head:
tail = tail.next
tail.next = head.next # unlink head
return head.next
prev.next = cur.next
return head
prev = cur
cur = cur.next
if cur is head:
break
return head
# example: delete 20 on [10, 20, 30]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 player who leaves a round-robin game
- Freeing a slot in a circular buffer
- Dynamic round-robin schedulers
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.