Circular Linked List in Data Structures – Operations, Examples & Code
Circular Linked List
🚀 Introduction
A Circular Linked List is a type of linked list where:
👉 The last node points back to the first node (head) instead of NULL.
This creates a loop (circle) in the list.
🔹 Structure
10 → 20 → 30 → 40
↑ ↓
←←←←←←←←←←←←←←←
👉 No NULL pointer in circular linked list.
🔹 Key Characteristics
- Last node connects to head
- Traversal can start from any node
- No NULL at the end
🔹 Operations in Circular Linked List
- Traversal
- Insertion
- Deletion
🔹 1. Traversal
💡 Concept
- Start from head
- Stop when you reach head again
🧑💻 Example
public function traverse() {
if ($this->head == null) return; $temp = $this->head; do {
echo $temp->data . " → ";
$temp = $temp->next;
} while ($temp != $this->head); echo "(back to head)";
}
⚡ Complexity
👉 O(n)
🔹 2. Insertion
✅ Insert at Beginning
💡 Concept
- Insert new node before head
- Update last node’s next to new node
🧑💻 Code
public function insertAtBeginning($data) {
$newNode = new Node($data); if ($this->head == null) {
$this->head = $newNode;
$newNode->next = $this->head;
return;
} $temp = $this->head; while ($temp->next != $this->head) {
$temp = $temp->next;
} $newNode->next = $this->head;
$temp->next = $newNode;
$this->head = $newNode;
}
✅ Insert at End
💡 Concept
- Traverse to last node
- Insert new node and link to head
🧑💻 Code
public function insertAtEnd($data) {
$newNode = new Node($data); if ($this->head == null) {
$this->head = $newNode;
$newNode->next = $this->head;
return;
} $temp = $this->head; while ($temp->next != $this->head) {
$temp = $temp->next;
} $temp->next = $newNode;
$newNode->next = $this->head;
}
🔹 3. Deletion
✅ Delete from Beginning
public function deleteFromBeginning() {
if ($this->head == null) return; if ($this->head->next == $this->head) {
$this->head = null;
return;
} $temp = $this->head; while ($temp->next != $this->head) {
$temp = $temp->next;
} $this->head = $this->head->next;
$temp->next = $this->head;
}
✅ Delete from End
public function deleteFromEnd() {
if ($this->head == null) return; if ($this->head->next == $this->head) {
$this->head = null;
return;
} $temp = $this->head; while ($temp->next->next != $this->head) {
$temp = $temp->next;
} $temp->next = $this->head;
}
⚡ Time Complexity
| Operation | Complexity |
|---|---|
| Traversal | O(n) |
| Insertion | O(n) |
| Deletion | O(n) |
🎯 Advantages
✅ No NULL pointers
✅ Efficient for circular applications
✅ Any node can be starting point
❌ Disadvantages
❌ Infinite loop risk
❌ More complex than singly linked list
🧠 Real-World Use Cases
- Music playlist (looping songs)
- Round-robin scheduling
- Multiplayer games turn system
🧠 Interview Tips
👉 Common questions:
- Detect circular linked list
- Split circular list
- Josephus problem
👉 Always remember:
👉 Use do-while loop instead of while loop
🏁 Conclusion
Circular Linked Lists are useful when data needs to be processed in a loop.
👉 They are widely used in real-world systems and interview problems.
No comments yet! You be the first to comment.
