DSAverse
Basics & Data Structures
Loading Data Structures...
Array Access
12
05
18
23
317
49
5array[0..5]indexed access
Array
Stack
Queue
Initializing Sorting Algorithms...
Sorting
Trees
Graphs
Preparing interactive visualizations...
Basics & Data Structures
Loading Data Structures...
Array Access
Array
Stack
Queue
The tail node's next pointer loops back to HEAD — no NULL terminator. Perfect for round-robin schedulers and circular buffers.
Every node has a next pointer. The tail wraps back to HEAD — no NULL at the end.
Color Legend
Insert, delete, search, or traverse to see the circular connection in action.
In a circular linked list, how do you detect that a traversal has visited all nodes?
class Node:
def __init__(self, value):
self.value = value
self.next = None
class CircularLinkedList:
def __init__(self):
self.head = None
self.tail = None
self.size = 0
def insert_head(self, value): # O(1)
node = Node(value)
if not self.head:
node.next = node # self-loop
self.head = self.tail = node
else:
node.next = self.head
self.tail.next = node # tail still points to new head
self.head = node
self.size += 1
def insert_tail(self, value): # O(1)
node = Node(value)
if not self.head:
node.next = node
self.head = self.tail = node
else:
node.next = self.head # new tail wraps to head
self.tail.next = node
self.tail = node
self.size += 1
def delete_head(self): # O(1)
if not self.head:
raise IndexError("Empty list")
val = self.head.value
if self.head == self.tail:
self.head = self.tail = None
else:
self.head = self.head.next
self.tail.next = self.head # tail now points to new head
self.size -= 1
return val
def traverse(self): # O(n) — stop at head
if not self.head:
return
cur = self.head
while True:
yield cur.value
cur = cur.next
if cur == self.head:
break
def search(self, value): # O(n)
if not self.head:
return -1
cur, idx = self.head, 0
while True:
if cur.value == value:
return idx
cur, idx = cur.next, idx + 1
if cur == self.head:
return -1