Circular Linked List (With C++ Examples)

A Circular Linked List (CLL) is a variation of the linked list in which the last node points back to the first node instead of NULL, forming a circular structure.

Unlike linear linked lists, a circular linked list has no definite beginning or end.

C++ Program to Implement Circular Doubly Linked List

📌 Definition

A Circular Linked List is a linked list where:

  • Each node contains data and a pointer

  • The last node’s next pointer points to the first node

  • Traversal can start from any node

Visit the previous lesson: Doubly Linked List Operations (With C++ Examples)

🧠 Types of Circular Linked List

  1. Circular Singly Linked List

  2. Circular Doubly Linked List

This section focuses on Circular Singly Linked List, which is most commonly used.

🧩 Structure of a Circular Linked List Node

struct Node {
     int data;
     Node* next;
};

🔍 Key Characteristics

  • No NULL pointer in the list

  • Last node links back to the head

  • Traversal must stop explicitly

  • Efficient for cyclic processes

🌍 Real-World Use Cases

  • Round-robin CPU scheduling

  • Multiplayer games (turn rotation)

  • Music playlists (repeat mode)

  • Traffic light systems

  • Circular buffers

Operations on Circular Linked List

1️⃣ Insert at the Beginning

📌 Explanation

A new node is inserted before the current head, and the last node is updated to point to the new head.

Insertion at the beginning in circular linked list - GeeksforGeeks

💻 C++ Code

void insertAtStart(Node*& head, int value) {
    Node* newNode = new Node();
    newNode->data = value;

    if (head == nullptr) {
        newNode->next = newNode;
        head = newNode;
        return;
    }

    Node* temp = head;
    while (temp->next != head)
        temp = temp->next;

    temp->next = newNode;
    newNode->next = head;
    head = newNode;
}

2️⃣ Insert at the End

📌 Explanation

The new node is inserted after the last node, and its next pointer is set to the head.

Insertion at the end in circular linked list - GeeksforGeeks

💻 C++ Code

void insertAtEnd(Node*& head, int value) {
    Node* newNode = new Node();
    newNode->data = value;

    if (head == nullptr) {
        newNode->next = newNode;
        head = newNode;
        return;
    }

    Node* temp = head;
    while (temp->next != head)
        temp = temp->next;

    temp->next = newNode;
    newNode->next = head;
}

3️⃣ Delete from the Beginning

📌 Explanation

The head node is removed, and the last node’s next pointer is updated.

Deletion from a Circular Linked List - GeeksforGeeks

💻 C++ Code

void deleteFromStart(Node*& head) {
    if (head == nullptr) return;

    if (head->next == head) {
        delete head;
        head = nullptr;
        return;
    }

    Node* temp = head;
    Node* last = head;

    while (last->next != head)
        last = last->next;

    head = head->next;
    last->next = head;
    delete temp;
}

4️⃣ Delete from the End

📌 Explanation

The last node is removed, and the second-last node is linked to the head.

Circular linked list implementation in javascript - LearnersBucket

💻 C++ Code

void deleteFromEnd(Node*& head) {
    if (head == nullptr) return;

    if (head->next == head) {
        delete head;
        head = nullptr;
        return;
    }

    Node* prev = nullptr;
    Node* curr = head;

    while (curr->next != head) {
        prev = curr;
        curr = curr->next;
    }

    prev->next = head;
    delete curr;
}

5️⃣ Display Circular Linked List

📌 Explanation

Traversal starts from the head and stops when the head is reached again.

Implementing Singly Circular Linked List in Golang | by Chandan Chiranjeeb Sahoo | Medium

💻 C++ Code

void display(Node* head) {
    if (head == nullptr) return;

    Node* temp = head;
    do {
        cout << temp->data << " ";
        temp = temp->next;
    } while (temp != head);
}

📊 Time Complexity Summary

       Operation            Time Complexity
Insert at Start O(n)
Insert at End O(n)
Delete from Start O(n)
Delete from End O(n)
Traversal O(n)

 

✅ Advantages

  • Efficient cyclic traversal

  • No NULL pointers

  • Ideal for round-robin systems

❌ Disadvantages

  • Complex logic

  • Risk of infinite loops

  • No direct backward traversal

🎯 Key Takeaway

A Circular Linked List is best suited for repetitive and cyclic tasks where data needs to be accessed continuously without restarting traversal.

Circular Linked List – Interview Questions & Answers

1. What is a circular linked list?

Answer:
A circular linked list is a linked list in which the last node points back to the first node instead of pointing to NULL, forming a circular structure.

2. How does a circular linked list differ from a singly linked list?

Answer:
In a singly linked list, the last node points to NULL, while in a circular linked list, the last node points to the head node.

3. Why is there no NULL pointer in a circular linked list?

Answer:
Because the list forms a closed loop, where every node points to another node, and the last node points back to the first node.

4. What are the main components of a circular linked list node?

Answer:
Each node contains:

  • Data

  • A pointer to the next node

5. How do you detect the end of traversal in a circular linked list?

Answer:
Traversal stops when the pointer reaches the starting node (head) again.

6. What is the time complexity of traversal in a circular linked list?

Answer:
O(n), where n is the number of nodes.

7. What happens if you forget the stopping condition while traversing?

Answer:
It results in an infinite loop, which can crash the program.

8. Where are circular linked lists used in real life?

Answer:
They are used in:

  • Round-robin CPU scheduling

  • Multiplayer turn-based games

  • Circular queues

  • Music playlists in repeat mode

9. Can you insert a node at the beginning of a circular linked list?

Answer:
Yes, but the last node’s next pointer must be updated to point to the new head.

10. What is the time complexity of inserting at the end?

Answer:
O(n) if no tail pointer is maintained.

11. How do you delete the first node in a circular linked list?

Answer:

  • Locate the last node

  • Move the head to the next node

  • Update the last node’s next pointer

  • Delete the old head

12. What is the advantage of a circular linked list over a singly linked list?

Answer:
It allows continuous traversal without restarting from the head.

13. What is a circular doubly linked list?

Answer:
A circular doubly linked list has:

  • Each node pointing to both previous and next nodes

  • The last node pointing to the first node

  • The first node pointing back to the last node

14. Can a circular linked list be empty?

Answer:
Yes, when the head pointer is set to NULL.

15. How do you count the number of nodes in a circular linked list?

Answer:
By traversing the list until the pointer returns to the head and incrementing a counter.

16. Why is a circular linked list suitable for round-robin scheduling?

Answer:
Because after the last process, control automatically returns to the first process without restarting traversal.

17. Is a circular linked list cache-friendly?

Answer:
No, because nodes are stored in non-contiguous memory locations.

18. What is the biggest risk when working with circular linked lists?

Answer:
The risk of infinite loops due to missing or incorrect termination conditions.

Are you interested in developing mobile application development skills? You may visit the link below:
https://onlineskilllab.com/2025/11/18/how-to-build-a-complete-flutter-authentication-system-with-firebase-login-signup-reset-password-dashboard/

If you are learning and enjoying, subscribe to our blog for daily updates.

Author

Write A Comment