0
0
PythonHow-ToBeginner · 4 min read

How to Implement Circular Linked List in Python: Simple Guide

A circular linked list in Python is a linked list where the last node points back to the first node, forming a circle. You implement it by creating nodes with a reference to the next node and making the last node's next point to the head node.
📐

Syntax

A circular linked list uses nodes where each node has a data value and a next pointer. The key is that the next of the last node points back to the first node, not None.

Typical parts:

  • Node class: Holds data and a pointer to the next node.
  • CircularLinkedList class: Manages nodes and keeps track of the head.
  • Insert method: Adds nodes and updates pointers to keep the circle.
python
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
        else:
            temp = self.head
            while temp.next != self.head:
                temp = temp.next
            temp.next = new_node
            new_node.next = self.head
💻

Example

This example shows how to create a circular linked list, add nodes, and print the list to confirm the circular structure.

python
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
        else:
            temp = self.head
            while temp.next != self.head:
                temp = temp.next
            temp.next = new_node
            new_node.next = self.head

    def display(self):
        nodes = []
        temp = self.head
        if not temp:
            return nodes
        while True:
            nodes.append(temp.data)
            temp = temp.next
            if temp == self.head:
                break
        return nodes

cll = CircularLinkedList()
cll.append(10)
cll.append(20)
cll.append(30)
print(cll.display())
Output
[10, 20, 30]
⚠️

Common Pitfalls

Common mistakes when implementing circular linked lists include:

  • Not linking the last node back to the head, breaking the circle.
  • Infinite loops when traversing if you forget to check for the head node.
  • Incorrectly handling the empty list case when adding the first node.

Always check if the list is empty before appending and use a loop that stops when you reach the head again.

python
class CircularLinkedList:
    def __init__(self):
        self.head = None

    # Wrong: Does not link last node to head
    def append_wrong(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
        else:
            temp = self.head
            while temp.next:
                temp = temp.next
            temp.next = new_node

    # Right: Links last node to head
    def append_right(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            new_node.next = self.head
        else:
            temp = self.head
            while temp.next != self.head:
                temp = temp.next
            temp.next = new_node
            new_node.next = self.head
📊

Quick Reference

  • Node: Stores data and next pointer.
  • Head: First node in the list.
  • Append: Add node and update last node's next to head.
  • Traversal: Stop when you reach head again to avoid infinite loop.

Key Takeaways

A circular linked list connects the last node back to the first node to form a loop.
Always update the last node's next pointer to the head when adding new nodes.
Check for an empty list before inserting the first node to avoid errors.
When traversing, stop when you reach the head again to prevent infinite loops.
Careful pointer updates are essential to maintain the circular structure.