0
0
DSA Pythonprogramming

Create a Circular Singly Linked List in DSA Python

Choose your learning style9 modes available
Mental Model
A circular singly linked list is like a chain where the last link connects back to the first, forming a circle.
Analogy: Imagine a round table where each person holds hands with the next, and the last person holds hands with the first, so the circle never breaks.
head -> 1 -> 2 -> 3 ->
↑                   ↓
←←←←←←←←←←←←←←←←←←←
Dry Run Walkthrough
Input: Create circular list with values 1, 2, 3 in order
Goal: Build a circular singly linked list where last node points back to head
Step 1: Create node with value 1 and set head to it
head -> [1] -> null
Why: Start the list with the first node
Step 2: Create node with value 2 and link it after node 1
head -> 1 -> [2] -> null
Why: Add second node after first to extend the list
Step 3: Create node with value 3 and link it after node 2
head -> 1 -> 2 -> [3] -> null
Why: Add third node after second to extend the list
Step 4: Link last node (3) back to head (1) to form circle
head -> 1 -> 2 -> 3 ->
↑                   ↓
←←←←←←←←←←←←←←←←←←←
Why: Close the list into a circle so traversal can loop
Result:
head -> 1 -> 2 -> 3 ->
↑                   ↓
←←←←←←←←←←←←←←←←←←←
Annotated Code
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

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

    def create(self, values):
        if not values:
            return
        self.head = Node(values[0])  # create head node
        curr = self.head
        for val in values[1:]:
            curr.next = Node(val)  # link new node after current
            curr = curr.next      # move current to new node
        curr.next = self.head      # link last node back to head

    def print_list(self):
        if not self.head:
            print("List is empty")
            return
        curr = self.head
        result = []
        while True:
            result.append(str(curr.data))
            curr = curr.next
            if curr == self.head:
                break
        print(" -> ".join(result) + " -> (back to head)")

# Driver code
cll = CircularSinglyLinkedList()
cll.create([1, 2, 3])
cll.print_list()
self.head = Node(values[0]) # create head node
initialize head with first value
curr.next = Node(val) # link new node after current
attach new node to current node's next
curr = curr.next # move current to new node
advance current pointer to newly added node
curr.next = self.head # link last node back to head
close the list into a circle by linking last node to head
while True: result.append(str(curr.data)) curr = curr.next if curr == self.head: break
traverse nodes until we loop back to head
OutputSuccess
1 -> 2 -> 3 -> (back to head)
Complexity Analysis
Time: O(n) because we create and link each of the n nodes once
Space: O(n) because we store n nodes in memory
vs Alternative: Compared to arrays, circular linked lists allow easy looping without index resets but require pointer management
Edge Cases
empty input list
no nodes created, list remains empty
DSA Python
if not values:
    return
single element list
head points to single node which points to itself
DSA Python
curr.next = self.head      # link last node back to head
When to Use This Pattern
When you need a list that loops back to start for continuous cycling, use a circular singly linked list to avoid resetting pointers manually.
Common Mistakes
Mistake: Forgetting to link the last node back to the head, leaving the list linear
Fix: Add the line that sets last node's next pointer to head after all nodes are created
Summary
Creates a linked list where the last node points back to the first, forming a circle.
Use when you want to cycle through elements repeatedly without stopping.
The key is linking the last node's next pointer back to the head node.