Data Structures · Visualization
Circular Linked List: Insert
Circular Linked List
Inserting into a circular linked list means linking the new node into the ring and always keeping the last node's next pointer directed at head. Time: Front/End with tail pointer: O(1); At index: O(n). Space: O(1) extra space per insert.
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: Insert
Circular Linked ListInserting into a circular linked list means linking the new node into the ring and always keeping the last node's next pointer directed at head. Insertion at the front, end, or a specific index each require re-pointing exactly two links.
How Insert Works
- Create the new node with the given value.
- For insert-at-front: point the new node's next to the current head, then update tail's next to the new node, then move head to the new node.
- For insert-at-end: point tail's next to the new node, point the new node's next back to head, then update tail to the new node.
- For insert-at-index: walk from head counting nodes until reaching the node before the target index, then splice the new node in between.
- Special case — inserting into an empty list: the new node becomes both head and tail, and points to itself.
Insert Worked Example
Input: Insert 15 at the front of ring 10 → 20 → 30 → (10)
- new = Node(15)
- new.next = head (10)
- tail(30).next = new (15)
- head = new (15)
Output: New ring: 15 → 10 → 20 → 30 → (back to 15)
Press ▶ Run to watch it step by step.
def insert_front(head, value):
node = Node(value)
if head is None: # empty list → self-loop
node.next = node
return node
tail = head
while tail.next is not head:
tail = tail.next
node.next = head # new head points to old
tail.next = node # tail keeps wrapping
return node
# head = insert_front(head, 9) → O(1) with a tail pointer
# example: insert at front 15 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.
Insert Time & Space Complexity
When to Use Insert
- Maintaining a rotating playlist
- Turn order lists in games
- Circular task 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.