#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Linked Lists

Circular Linked List in Data Structure

A Circular Linked List is a variation of a linked list in which the last node points back to the first node instead of pointing to None, forming a circular chain.

Types

  • Singly Circular Linked List: each node points to the next, and the last node points back to the head.
  • Doubly Circular Linked List: each node has both next and previous pointers, and the list forms a loop in both directions.
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class CircularLinkedList:
    def __init__(self):
        self.head = None

    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            new_node.next = self.head
            return
        current = self.head
        while current.next != self.head:
            current = current.next
        current.next = new_node
        new_node.next = self.head

    def traverse(self):
        if not self.head:
            return []
        values = [self.head.data]
        current = self.head.next
        while current != self.head:
            values.append(current.data)
            current = current.next
        return values

Complexity

OperationTime
Insert at endO(n) without tail pointer, O(1) with tail pointer
TraversalO(n)

Applications

  • Round-robin CPU scheduling.
  • Implementing circular buffers for streaming data.
  • Multiplayer turn-based games cycling through players.
  • Managing playlists that loop back to the first song.

The Tail Pointer Trick for Constant-Time Inserts

In a circular list, the last node already points to the first one. So if you keep a reference to the tail instead of the head, you get both ends for free: the tail is tail and the head is tail.next. This makes inserting at the front and at the end constant-time operations.

class CircularList:
    def __init__(self):
        self.tail = None

    def insert_front(self, data):
        node = Node(data)
        if self.tail is None:
            node.next = node            # a single node points to itself
            self.tail = node
        else:
            node.next = self.tail.next  # new node points to old head
            self.tail.next = node       # tail now points to new head

    def insert_end(self, data):
        self.insert_front(data)
        self.tail = self.tail.next      # the new head becomes the tail

    def delete(self, key):
        if self.tail is None:
            return False
        prev, cur = self.tail, self.tail.next
        while True:
            if cur.data == key:
                if cur is prev:          # it was the only node
                    self.tail = None
                else:
                    prev.next = cur.next
                    if cur is self.tail:
                        self.tail = prev
                return True
            prev, cur = cur, cur.next
            if cur is self.tail.next:    # back at the head: not found
                return False

Notice how insert_end reuses insert_front: in a ring, adding before the head and then moving the tail one step forward places the new node at the end.

Stopping Conditions: Avoiding the Infinite Loop

A normal linked list ends at None, so while current: stops on its own. A circular list never reaches None, and that same loop runs forever. Always stop when you return to the node you started from, and handle the empty list before the loop begins. A do-while style loop (run once, then check) is the cleanest pattern, which is why the delete method above uses while True with a check at the end.

Worked Example: The Josephus Problem

This classic puzzle fits a circular list perfectly. People stand in a circle, and every k-th person is removed until one remains. Take 5 people numbered 1 to 5, removing every second person:

  1. Count 1, 2 from person 1: person 2 is removed.
  2. Count from person 3: person 4 is removed.
  3. Count from person 5: person 1 is removed.
  4. Count from person 3: person 5 is removed.
  5. Person 3 is the survivor.

With a circular list, each removal is just relinking one pointer, and the counting wraps around naturally without any index arithmetic.

Picking Between Linear and Circular Lists

NeedBetter choice
Repeatedly cycle through items in turnCircular list
Quick access to both first and last node with one pointerCircular list with a tail reference
Simple traversal that ends naturallyLinear singly linked list
Frequent movement backwardsDoubly circular list

Common mistakes

  • Forgetting that a one-node list points to itself, then setting its next to None.
  • Deleting the tail node without moving the tail reference back.
  • Printing the list with a loop that never checks for the starting node.

Checklist: handle empty and single-node cases first, track the tail, and always stop when you return to the start.