Course contentsShow
← Computer Science
Lesson 351 of 2,8728. Linked Data StructuresFree lesson

Insertion in Circular Linked Lists

Implementing insertion operations at beginning, end, and middle positions while maintaining the circular structure.

Insertion in Circular Linked Lists

What you'll learn: How to add new nodes to a circular linked list while preserving its continuous, circular structure.

The Core Idea

In a circular linked list, the last node points back to the first node instead of pointing to NULL. When inserting a new node, you must maintain this loop—no matter where you insert (beginning, end, or middle), the circle must stay unbroken.

Three Insertion Scenarios

Inserting at the Beginning

To insert at the head:

  1. Create your new node
  2. Make it point to the current head
  3. Find the last node (traverse until you reach the node pointing to head)
  4. Update the last node to point to your new node
  5. Update the head pointer

The tricky part: you must find and update that last node to complete the circle.

Inserting at the End

To insert at the tail:

  1. Create your new node
  2. Traverse to the current last node (the one pointing to head)
  3. Make the last node point to your new node
  4. Make your new node point back to the head

This effectively adds a new "last" node while maintaining the circular connection.

Inserting in the Middle

To insert at a specific position:

  1. Traverse to the node just before your target position
  2. Make your new node point to the next node
  3. Update the previous node to point to your new node

This is similar to singly linked lists, but you never worry about NULL—the circle ensures there's always a valid next node.

Special Case: Empty List

When the list is empty, your new node should point to itself, creating a single-node circle.

Key Takeaway: Insertion in circular linked lists requires maintaining the circular connection by ensuring the last node always points back to the head, never leaving any NULL pointers in the structure.