Circular Linked List Implementation in Python

1. Introduction

A Circular Linked List (CLL) is a variation of the standard linked list where the last node of the list points back to the first node instead of having a null reference. This circular structure can be useful in scenarios where we need a cyclic iteration through the elements.

2. Implementation Steps

1. Define a Node class that contains the data and a reference to the next node.

2. Implement the CircularLinkedList class with a reference to the head node.

3. Create methods to append, prepend, remove, and display the list.

4. Ensure that the tail's next pointer always points back to the head to maintain the circular structure.

3. Implementation in Python

class Node:
    def __init__(self, data): = data         # Holds the data of the node = None         # Pointer to the next node
class CircularLinkedList:
    def __init__(self):
        self.head = None         # Initialize the head of the linked list
    def append(self, data):
        # Append data to the end of the linked list
        new_node = Node(data)
        if not self.head:
            self.head = new_node
   = self.head
            temp = self.head
            while != self.head:
                temp =
   = new_node
   = self.head
    def prepend(self, data):
        # Add data to the beginning of the linked list
        new_node = Node(data) = self.head
        temp = self.head
        while != self.head:
            temp = = new_node
        self.head = new_node
    def remove(self, data):
        # Remove a node with the given data
        current = self.head
        prev = None
        while current:
            if == data:
                if prev:
                    if current == self.head:
                        self.head =
                    next_node =
                    if current == self.head:
                        while != self.head:
                            current =
               = next_node
                        self.head = next_node
            prev = current
            current =
            if current == self.head:
    def display(self):
        # Print the linked list
        nodes = []
        current_node = self.head
        while True:
            current_node =
            if current_node == self.head:
        print(" -> ".join(map(str, nodes)) + " -> ...")
# Test the CircularLinkedList class
cllist = CircularLinkedList()


D -> A -> B -> C -> ...
D -> A -> C -> ...


1. The Node class for a circular linked list has the data and a pointer to the next node.

2. The CircularLinkedList class keeps a reference to the head node.

3. In the append method, we loop until we find the last node (which has a next pointer to the head) and add our new node there.

4. The prepend method involves adjusting both the new node's next pointer and the tail's next pointer to ensure a circular structure.

5. The remove method deletes nodes while considering edge cases to maintain the circular nature.

6. The display method prints elements in the list and ends with '...' to indicate the cyclic nature.

7. In the test, we create a list, append and prepend nodes, and then remove a node to demonstrate the circular linked list's functionality.

Related Data Structures in Python

  1. Stack Implementation in Python
  2. Queue Implementation in Python
  3. Deque Implementation in Python
  4. Singly Linked List Implementation in Python
  5. Doubly Linked List Implementation in Python
  6. Circular Linked List Implementation in Python
  7. PriorityQueue Implementation in Python
  8. Circular Queue Implementation in Python
  9. Binary Search Tree Implementation in Python
  10. Stack Implementation Using Linked List in Python
  11. Stack Implementation Using Doubly Linked List in Python