Tag: circular doubly linked list

  • Circular Linked List | Master Interview ready Guide (2026)

    Last Updated: 31 Aug, 2025

    Beginner-friendly guide to Circular Linked List. Learn types, structure, advantages, disadvantages, applications, and C code examples. When learning data structures, most beginners start with arrays and linked lists. Among different types of linked lists, the Circular Linked List is an important concept that offers flexibility in memory management and traversal. In this article, we will explain what a circular linked list is, its types, advantages, and real-world applications in a beginner-friendly way.

    A Circular Linked List is a special type of linked list in which the last node is connected back to the first node, forming a closed loop. Unlike arrays, circular linked lists are dynamic in nature, meaning they can grow or shrink during program execution.

    What is a Circular Linked List?

    A Circular Linked List is a type of linked list where the last node points back to the first node instead of pointing to NULL. This creates a circular connection, allowing continuous traversal of the list in a loop.

    Unlike a Singly Linked List, which ends at NULL, or a Doubly Linked List, which has two pointers, the circular linked list makes navigation seamless without an endpoint.

    Types of Circular Linked List

    Singly Circular Linked List

    A singly circular linked list is just like a normal singly linked list, but with one small twist. In this structure, every node has two parts: the data it stores and a pointer to the next node. The difference is that the last node doesn’t point to NULL; instead, it connects back to the very first node. This creates a circular chain where you can keep moving forward from one node to the next without ever reaching an end. Imagine it like kids standing in a circle holding hands—if you keep walking around, you’ll return to the starting point. This kind of list is useful when you need to repeatedly go through data, such as in round-robin scheduling or playlist management in music players.

    Singly Circular Linked List
    Singly Circular Linked List

    Doubly Circular Linked List

    A doubly circular linked list is an advanced version of the circular linked list. In this structure, every node contains three parts: the data, a pointer to the next node, and another pointer to the previous node. Unlike a simple doubly linked list, the last node doesn’t end with NULL. Instead, its next pointer connects back to the first node, and the first node’s previous pointer links to the last node. This creates a complete circular chain where you can move forward or backward through the list without ever reaching an endpoint. You can think of it like a train running on a circular track—you can travel clockwise or counterclockwise and always come back to where you started. This makes doubly circular linked lists very handy in situations like navigation menus, music or video playlists, and simulations where movement in both directions is needed.

    Doubly Circular Linked List
    Doubly Circular Linked List

    Basic Structure of a Circular Linked List

    Here’s a simple structure in C language:

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

    For a Doubly Circular Linked List, we add a previous pointer:

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

    What is Node Representation of Circular Linked List

    class Node {
    public:
        int data;
        Node* next;
    
        Node(int value) {
            data = value;
            next = nullptr;
        }
    };
    struct Node {
    int data;
    struct Node* next;
    };

    Operations on Circular Linked List

    1. Insertion
      • At the beginning
      • At the end
      • At a given position
    2. Deletion
      • From the beginning
      • From the end
      • From a given position
    3. Traversal
      • Visit all nodes continuously in a loop.

    Circular Linked List Implementations

    C++ Implementation of Circular Linked List

    #include <iostream>
    using namespace std;
    
    class Node {
    public:
        int data;
        Node* next;
    
        Node(int value) {
            data = value;
            next = nullptr;
        }
    };
    
    class CircularLinkedList {
    private:
        Node* last;
    public:
        CircularLinkedList() { last = nullptr; }
    
        void insertEnd(int value) {
            Node* newNode = new Node(value);
            if (last == nullptr) {
                last = newNode;
                last->next = last;
            } else {
                newNode->next = last->next;
                last->next = newNode;
                last = newNode;
            }
        }
    
        void display() {
            if (last == nullptr) return;
            Node* temp = last->next;
            do {
                cout << temp->data << " -> ";
                temp = temp->next;
            } while (temp != last->next);
            cout << "(back to start)" << endl;
        }
    };
    
    int main() {
        CircularLinkedList cll;
        cll.insertEnd(10);
        cll.insertEnd(20);
        cll.insertEnd(30);
        cll.display();
        return 0;
    }
    

    Output:

    10 -> 20 -> 30 -> (back to start)
    

    Java Implementation of Circular Linked List

    class Node {
        int data;
        Node next;
    
        Node(int value) {
            data = value;
            next = null;
        }
    }
    
    class CircularLinkedList {
        private Node last;
    
        CircularLinkedList() {
            last = null;
        }
    
        void insertEnd(int value) {
            Node newNode = new Node(value);
            if (last == null) {
                last = newNode;
                last.next = last;
            } else {
                newNode.next = last.next;
                last.next = newNode;
                last = newNode;
            }
        }
    
        void display() {
            if (last == null) return;
            Node temp = last.next;
            do {
                System.out.print(temp.data + " -> ");
                temp = temp.next;
            } while (temp != last.next);
            System.out.println("(back to start)");
        }
    
        public static void main(String[] args) {
            CircularLinkedList cll = new CircularLinkedList();
            cll.insertEnd(10);
            cll.insertEnd(20);
            cll.insertEnd(30);
            cll.display();
        }
    }
    

    Output:

    10 -> 20 -> 30 -> (back to start)
    

    Python Implementation of Circular Linked List

    class Node:
        def __init__(self, data):
            self.data = data
            self.next = None
    
    class CircularLinkedList:
        def __init__(self):
            self.last = None
    
        def insert_end(self, value):
            new_node = Node(value)
            if self.last is None:
                self.last = new_node
                self.last.next = self.last
            else:
                new_node.next = self.last.next
                self.last.next = new_node
                self.last = new_node
    
        def display(self):
            if self.last is None:
                return
            temp = self.last.next
            while True:
                print(temp.data, end=" -> ")
                temp = temp.next
                if temp == self.last.next:
                    break
            print("(back to start)")
    
    # Example usage
    cll = CircularLinkedList()
    cll.insert_end(10)
    cll.insert_end(20)
    cll.insert_end(30)
    cll.display()
    

    Output:

    10 -> 20 -> 30 -> (back to start)
    

    JavaScript Implementation of Circular Linked List

    class Node {
      constructor(data) {
        this.data = data;
        this.next = null;
      }
    }
    
    class CircularLinkedList {
      constructor() {
        this.last = null;
      }
    
      insertEnd(value) {
        let newNode = new Node(value);
        if (this.last === null) {
          this.last = newNode;
          this.last.next = this.last;
        } else {
          newNode.next = this.last.next;
          this.last.next = newNode;
          this.last = newNode;
        }
      }
    
      display() {
        if (this.last === null) return;
        let temp = this.last.next;
        let result = "";
        do {
          result += temp.data + " -> ";
          temp = temp.next;
        } while (temp !== this.last.next);
        console.log(result + "(back to start)");
      }
    }
    
    // Example usage
    let cll = new CircularLinkedList();
    cll.insertEnd(10);
    cll.insertEnd(20);
    cll.insertEnd(30);
    cll.display();
    

    Output:

    10 -> 20 -> 30 -> (back to start)

    Advantages of Circular Linked List

    • Efficient Memory Usage – No need for a NULL pointer.
    • Continuous Traversal – Useful in applications where we need to cycle through data repeatedly.
    • Dynamic Size – Unlike arrays, circular linked lists can grow and shrink easily.
    • Queue Implementation – Particularly helpful in designing circular queues.
    • Saves memory – No NULL pointer
    • Uniform traversal in a loop.

    Disadvantages of Circular Linked List

    • Complex Implementation – Requires extra care while inserting and deleting nodes.
    • Infinite Loops – Careless traversal may lead to infinite looping.
    • More Memory Per Node – Extra pointers (in the case of a doubly circular linked list).
    • Requires more memory in case of doubly circular list.

    Applications of Circular Linked List

    • Operating Systems – Used in CPU scheduling (Round-Robin Scheduling).
    • Music or Video Playlists – When songs or videos repeat in a loop.
    • Multi-Player Games – Switching turns between players in a continuous cycle.
    • Data Buffers – Implementing circular queues and memory buffers.
    • Traffic Systems → Repeated light cycles.

    Traversing a Circular Linked List in C

    #include <stdio.h>
    #include <stdlib.h>
    
    struct Node {
        int data;
        struct Node* next;
    };
    
    void traverse(struct Node* head) {
        struct Node* temp = head;
        if (head != NULL) {
            do {
                printf("%d -> ", temp->data);
                temp = temp->next;
            } while (temp != head);
        }
    }
    
    int main() {
        struct Node* head = malloc(sizeof(struct Node));
        struct Node* second = malloc(sizeof(struct Node));
        struct Node* third = malloc(sizeof(struct Node));
    
        head->data = 10; head->next = second;
        second->data = 20; second->next = third;
        third->data = 30; third->next = head;
    
        traverse(head);
        return 0;
    }
    

    Output:

    10 -> 20 -> 30 ->
    

    Insertion at the End of Circular Linked List

    #include <iostream>
    using namespace std;
    
    class Node {
    public:
        int data;
        Node* next;
    
        Node(int value) {
            data = value;
            next = nullptr;
        }
    };
    
    void insertEnd(Node*& last, int value) {
        Node* newNode = new Node(value);
    
        if (last == nullptr) {
            last = newNode;
            last->next = last; // Self loop
        } else {
            newNode->next = last->next;
            last->next = newNode;
            last = newNode;
        }
    }
    
    void display(Node* last) {
        if (last == nullptr) return;
    
        Node* temp = last->next;
        do {
            cout << temp->data << " -> ";
            temp = temp->next;
        } while (temp != last->next);
        cout << " (back to start)\n";
    }
    
    int main() {
        Node* last = nullptr;
    
        insertEnd(last, 10);
        insertEnd(last, 20);
        insertEnd(last, 30);
    
        display(last);
        return 0;
    }

    Why Learn Circular Linked List?

    • It helps in CPU scheduling algorithms (Round Robin).
    • Useful in repeated task execution like music playlists, traffic signal management, and multiplayer games.
    • Provides constant time insertion at both ends.

    Difference Between Circular Linked List and Regular Linked List

    FeatureRegular Linked ListCircular Linked List
    Last NodePoints to NULLPoints to first node
    TraversalStops at NULLNever ends, continues in a loop
    Memory UsageHas a terminating pointerUtilizes memory efficiently
    ApplicationsBasic data storageScheduling, queues, buffers

    When to Use Circular Linked List Instead of Array?

    Use CaseArrayCircular Linked List
    Fixed size✅❌
    Dynamic growth❌✅
    Random access✅ O(1)❌ O(n)
    Continuous looping❌✅
    Insertion at beginning❌ O(n)✅ O(1)

    Circular Linked List Interview Questions

    Preparing for coding interviews? Here’s a categorized list of Circular Linked List interview questions suitable for beginners, intermediate learners, and experienced professionals.

    Beginner-Level Questions

    1. What is a Circular Linked List?
    2. How does a Circular Linked List differ from a regular Linked List?
    3. What are the types of Circular Linked Lists?
    4. Write the structure of a node in a Circular Singly Linked List.
    5. Write the structure of a node in a Circular Doubly Linked List.
    6. What are the advantages of Circular Linked Lists over arrays?
    7. List some real-world applications of Circular Linked Lists.
    8. What is the time complexity of insertion at the beginning of a Circular Linked List?
    9. What is the time complexity of deletion at the end of a Circular Linked List?
    10. Explain how traversal works in a Circular Linked List.

    Intermediate-Level Questions

    1. Why do we often maintain a pointer to the last node instead of the first node in Circular Linked Lists?
    2. Explain insertion at the beginning, end, and a specific position in a Circular Linked List.
    3. Explain deletion at the beginning, end, and a specific position in a Circular Linked List.
    4. How do you implement a Circular Queue using a Circular Linked List?
    5. How would you check if a linked list is circular?
    6. What is the difference between a Circular Singly Linked List and a Circular Doubly Linked List?
    7. Write pseudocode for traversing a Circular Linked List.
    8. How can you implement Round Robin Scheduling using a Circular Linked List?
    9. Compare time complexities of insertion and deletion in Circular Linked Lists vs Arrays.
    10. What challenges arise while debugging a Circular Linked List?

    Experienced-Level Questions

    1. How do you detect an infinite loop in a Circular Linked List?
    2. How would you reverse a Circular Linked List?
    3. How can a Circular Linked List be used to implement a deque (double-ended queue)?
    4. In which scenarios is a Circular Linked List better than a Doubly Linked List?
    5. How would you merge two Circular Linked Lists into one?
    6. Design an algorithm to split a Circular Linked List into two halves.
    7. How do you handle memory management issues in large Circular Linked Lists?
    8. What are the potential pitfalls in pointer manipulation when inserting nodes into a Circular Linked List?
    9. How does a Circular Linked List improve CPU scheduling performance?
    10. Design a Circular Linked List to handle a real-time system scenario (e.g., traffic light control).

    Conclusion

    A Circular Linked List is a powerful data structure for situations requiring continuous traversal and dynamic memory allocation. Though slightly more complex than singly linked lists, its applications in operating systems, games, and memory management make it worth learning.

    If you are a beginner, start by understanding the structure, then practice insertion, deletion, and traversal operations in circular linked lists. With time, you’ll find this data structure highly useful in real-world applications.

    If you are a beginner, start with circular singly linked lists and then move on to doubly circular linked lists. With practice, you’ll understand how this structure outperforms arrays and regular linked lists in certain scenarios.

    FAQs on Circular Linked List

    1.What is the main difference between singly and circular singly linked list?

    Ans: In singly linked list, traversal ends at NULL. In circular singly linked list, traversal loops back to the first node.

    2.Why is circular linked list used in OS scheduling?

    Ans: Because tasks can be executed in a continuous loop (Round Robin algorithm).

    3.Can we implement queues using circular linked list?

    Ans: Arrays are faster for random access, but circular linked lists are better for dynamic memory and looping tasks. Yes, circular linked lists are best suited for circular queues.

    4.Which is faster — array or circular linked list?

    Ans: Arrays are faster for random access, but circular linked lists are better for dynamic memory and looping tasks.

    5.Why is it better to keep a pointer to the last node instead of the first node in a Circular Linked List?

    Ans: Because insertion at the beginning and the end can be done in O(1) time without traversing the entire list.

    6.How do you detect an infinite loop in a Circular Linked List?

    Ans: By using the Floyd’s Cycle Detection Algorithm (Tortoise and Hare algorithm).

    7.What is the time complexity for insertion and deletion in Circular Linked List?

    Ans: Insertion at beginning/end → O(1)
    Insertion at specific position → O(n)
    Deletion at beginning/end → O(1)
    Deletion at specific position → O(n)

    8.What are the challenges in debugging a Circular Linked Lis

    Ans: Risk of infinite loops if termination conditions are not handled.
    Requires extra care in insertion/deletion to maintain circular structure.

    9.Compare Circular Linked List with Doubly Linked List in terms of memory usage and traversal.

    Ans: Circular Linked List saves memory by avoiding NULL pointers.
    Doubly Circular Linked List requires more memory but supports two-way traversal.

    10.How does a Circular Linked List improve CPU scheduling in an Operating System?

    Ans: In Round Robin Scheduling, processes are arranged in a circular linked list so the CPU can allocate time slices fairly to each process in a loop.
    Circular Linked List
    Circular Linked List Master Interview ready Guide (2025)
  • Master Doubly Linked List in Data Structures (With Examples) 2026

    Introduction to Doubly Linked List

    When you’re learning data structures, you’ll often hear about Linked Lists. A Doubly Linked List (DLL) is a powerful and flexible variation of the basic Singly Linked List that allows you to traverse in both directions — forward and backward.

    In this beginner-friendly article, we’ll explore:

    • What is a Doubly Linked List?
    • How it works (with diagrams)
    • Real-world uses
    • Advantages over singly linked list
    • Basic operations (insertion, deletion, traversal)
    • Code example in C/C++

    🚀 Whether you’re preparing for coding interviews or improving your DSA skills, understanding doubly linked lists is a must!

    What is a Doubly Linked List?

    A Doubly Linked List is a type of linear data structure made up of nodes, where each node contains:

    • Data (the actual value)
    • Pointer to the next node
    • Pointer to the previous node

    ⚙️ Node Structure of Doubly Linked List:

    +-------+----------+----------+
    | prev  |   data   |   next   |
    +-------+----------+----------+
    

    Each node is connected to its previous and next node, unlike a singly linked list, which only has a next pointer.

    Visual Representation of Doubly Linked List

    Let’s say we have 3 nodes with data: 10, 20, and 30.

    NULL <- [10] <-> [20] <-> [30] -> NULL
    

    Here:

    • Node 10’s prev is NULL
    • Node 20’s prev points to 10 and next to 30
    • Node 30’s next is NULL

    Basic Operations on Doubly Linked List

    1. Insertion

    • At the beginning
    • At the end
    • At a specific position

    2. Deletion

    • From the beginning
    • From the end
    • Specific node

    3. Traversal

    • Forward traversal (left to right)
    • Backward traversal (right to left)

    C Code Example: Basic DLL Operations

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct Node {
        int data;
        struct Node* prev;
        struct Node* next;
    } Node;
    
    Node* head = NULL;
    
    // Insert at end
    void insertEnd(int value) {
        Node* newNode = (Node*)malloc(sizeof(Node));
        newNode->data = value;
        newNode->next = NULL;
    
        if (head == NULL) {
            newNode->prev = NULL;
            head = newNode;
            return;
        }
    
        Node* temp = head;
        while (temp->next != NULL)
            temp = temp->next;
    
        temp->next = newNode;
        newNode->prev = temp;
    }
    
    // Display forward
    void displayForward() {
        Node* temp = head;
        printf("DLL (forward): ");
        while (temp != NULL) {
            printf("%d <-> ", temp->data);
            temp = temp->next;
        }
        printf("NULL\n");
    }
    
    // Display backward
    void displayBackward() {
        Node* temp = head;
        if (!temp) return;
        
        // Go to last node
        while (temp->next != NULL)
            temp = temp->next;
    
        printf("DLL (backward): ");
        while (temp != NULL) {
            printf("%d <-> ", temp->data);
            temp = temp->prev;
        }
        printf("NULL\n");
    }
    
    int main() {
        insertEnd(10);
        insertEnd(20);
        insertEnd(30);
        
        displayForward();
        displayBackward();
        return 0;
    }
    

    Advantages of Doubly Linked List

    • Two-way traversal: You can navigate forward and backward.
    • Easier deletion: You can delete a node without needing the previous node pointer.
    • More flexible: Especially useful in complex data structures like Deques, Undo/Redo systems, Music Playlists, etc.

    Disadvantages of Doubly Linked List

    • More memory: Each node requires extra space for the prev pointer.
    • More complex: Handling pointers can be error-prone for beginners.

    Real-Life Applications of Doubly Linked List

    • Web browsers (backward and forward navigation)
    • Music playlist apps
    • Text editors (undo/redo operations)
    • Task managers (circular doubly linked lists for round-robin)

    Conclusion DLL

    Doubly Linked List
    Master Doubly Linked List in Data Structures (With Examples) 2025

    The Doubly Linked List is an essential topic in Data Structures and Algorithms (DSA). It offers a better way to manage dynamic data, especially when two-way traversal is needed. Though slightly more complex than a singly linked list, the benefits are powerful.

    Whether you’re preparing for a coding interview, building a system-level application, or just learning C programming, mastering DLLs will give you a strong foundation in memory management and pointer logic.

    FAQs of Doubly Linked List

    Q1: Is doubly linked list faster than singly linked list?
    Ans: DLL allows backward traversal and easier deletion, but it uses more memory. It’s more flexible, not necessarily faster.

    Q2: Where is DLL used in real life?
    Ans: Browser history, playlist navigation, undo/redo features, and memory management systems.

    Q3: What is the main drawback of doubly linked list?
    Ans: It uses extra memory due to the additional prev pointer and increases code complexity.

    Q4: How is a doubly linked list different from a circular doubly linked list?
    Ans: In a doubly linked list, the last node’s next pointer is NULL, whereas in a circular doubly linked list, the last node’s next points back to the head, forming a circle.

    Q5: Can a doubly linked list be empty?
    Ans: Yes, initially a doubly linked list can be empty with head pointing to NULL.

    Q6: How do you insert a node at the beginning of a doubly linked list?
    Ans: Create a new node, point its next to the current head, set the head’s prev to the new node, and then update head to the new node.

    Q7: What happens when you delete the last node in a doubly linked list?
    Ans: The second last node’s next pointer is set to NULL, and the last node is freed from memory.

    Q8: Is it possible to implement a stack using a doubly linked list?
    Ans: Yes, a doubly linked list can be used to implement a stack with push and pop operations.

    Q9: How does traversal in a doubly linked list work?
    Ans: You can traverse forward from head to NULL using next pointers or backward from the last node to head using prev pointers.

    Q10: What are the memory requirements for a doubly linked list node?
    Ans: Each node stores data plus two pointers (prev and next), so it uses more memory compared to a singly linked list node, which stores only one pointer.

    Q11: Can doubly linked lists have cycles? How to detect them?
    Ans: Yes, cycles can exist if nodes point back to earlier nodes. Cycle detection algorithms like Floyd’s Cycle-Finding Algorithm (tortoise and hare) can detect cycles.

    Q12: Are doubly linked lists used in databases?
    Ans: Yes, doubly linked lists are used to implement certain data structures like LRU caches, which are common in databases and operating systems.

    Q13: How do you reverse a doubly linked list?
    Ans: Swap the prev and next pointers for each node and update the head to the last node.