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
| Operation | Time |
|---|---|
| Insert at end | O(n) without tail pointer, O(1) with tail pointer |
| Traversal | O(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.
PreviousDoubly Linked List in Data Structure with Example
Next Dijkstra's Algorithm in Data Structure
Ready to master Data Structures & Algorithms?
Learn DSA hands-on with mentor-led sessions, real interview practice, and placement support.
.png)