Search⌘ K
AI Features

Circular Linked Lists Operations

Explore the core operations of circular linked lists in C#, including traversal, insertion at various positions, and deletion. Understand how to maintain the circular connection during each operation and handle edge cases like a single-node list. This lesson equips you to implement and manipulate circular linked lists efficiently for practical applications.

At this stage, the difference between a circular linked list and a standard singly linked list is established. Instead of the last node pointing to null, it points back to the head node, forming a loop. As a result, the list has no natural termination point, and traversal requires explicit stopping conditions.

Now, let’s focus on the operations that can be performed on circular linked lists. While many of these operations are conceptually similar to those used in singly linked lists, there is an important difference: the circular connection must always be preserved.

Because the list forms a loop, operations such as traversal, insertion, and deletion must be implemented carefully. In particular:

  • Traversal cannot rely on reaching null to stop.

  • Insertions must ensure that the circular link remains intact.

  • Deletions must reconnect neighboring nodes so that the loop structure is maintained.

Traversal in a circular linked list

Traversal in a circular linked list is slightly different from traversal in a standard singly linked list. In a regular linked list, traversal stops when the current node becomes null. In a circular linked list, this stopping condition does not exist because the last node points back to the head.

Instead, traversal must stop when it returns to the starting node. Typically, the traversal steps are:

  1. Start traversal from the head node.

  2. Print the value stored in the current node.

  3. Move to the next node using current.Next.

  4. Stop the traversal when the pointer reaches the head node again.

Let's look at the following interactive visualizer for circular linked list traversal.

C# implementation of traversal in a circular linked list

The following code creates a simple circular linked list and traverses it once, stopping when the traversal returns to the head node.

C# 14.0
using System;
class ListNode
{
public int Data;
public ListNode Next; // Points to the next node
public ListNode(int data)
{
Data = data;
Next = null;
}
}
class CircularLinkedList
{
public ListNode Head; // Points to the first node
public void Traverse()
{
if (Head == null)
{
Console.WriteLine("The list is empty.");
return;
}
ListNode current = Head; // Start from the head node
while (true)
{
Console.Write(current.Data + " -> "); // Process current node
current = current.Next!; // Move to the next node
if (current == Head) // Stop when traversal returns to head
{
break;
}
}
Console.WriteLine(current.Data + " (back to head)");
}
}
class Program
{
static void Main()
{
// Create a circular linked list manually: 10 -> 20 -> 30 -> back to 10
CircularLinkedList sampleList = new CircularLinkedList();
ListNode first = new ListNode(10);
ListNode second = new ListNode(20);
ListNode third = new ListNode(30);
sampleList.Head = first;
first.Next = second;
second.Next = third;
third.Next = first; // Make the list circular
// Traverse the circular linked list
Console.Write("Traversal of circular linked list: ");
sampleList.Traverse();
}
}
Circular linked list traversal
  • Time complexity: O(n)O(n) because each node is visited once.

  • Space complexity: O(1)O(1) as traversal only uses a single reference variable.

Insertion in a circular linked list

Insertion in a circular linked list is similar to insertion in a singly linked list, but there is one extra requirement: the circular connection must remain intact. ...