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 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. Insertion at the front, end, or a specific index each require re-pointing exactly two links.

How Insert Works

  1. Create the new node with the given value.
  2. 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.
  3. 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.
  4. For insert-at-index: walk from head counting nodes until reaching the node before the target index, then splice the new node in between.
  5. 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)

  1. new = Node(15)
  2. new.next = head (10)
  3. tail(30).next = new (15)
  4. head = new (15)

Output: New ring: 15 → 10 → 20 → 30 → (back to 15)

Try your own input
Insert at FrontO(1)
New node points to the old head; the tail is rewired to the new node.
10➜
→
20➜
→
30↺
↔ back to head 10
Active / pointers ↺ tail → head (no null)
Insert at Front
Press ▶ Run to watch it step by step.
Python · Circular Linked Lists in Code
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]
Input: Insert 15 at the front of ring 10 → 20 → 30 → (10)

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

TimeFront/End with tail pointer: O(1); At index: O(n)
SpaceO(1) extra space per insert

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

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.