Python program to insert a new node at the middle of the Circular Linked List

When it is required to insert a new node at the middle of the circular linked list, a Node class needs to be created. In this class, there are two attributes: the data that is present in the node, and the access to the next node of the linked list.

In a circular linked list, the head and the rear are adjacent to each other. They are connected to form a circle, and don't have 'NULL' value in the last node. Instead, the last node points back to the first node.

Node Class Structure

The Node class contains the data and a pointer to the next node ?

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

Circular Linked List Implementation

Here's a complete implementation that demonstrates inserting nodes at the middle of a circular linked list ?

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class CircularLinkedList:
    def __init__(self):
        self.head = None
        self.tail = None
        self.size = 0

    def add_data(self, data):
        new_node = Node(data)
        if self.head is None:
            self.head = new_node
            self.tail = new_node
            new_node.next = self.head
        else:
            self.tail.next = new_node
            self.tail = new_node
            self.tail.next = self.head
        self.size += 1

    def add_in_middle(self, data):
        new_node = Node(data)
        if self.head is None:
            self.head = new_node
            self.tail = new_node
            new_node.next = self.head
        else:
            # Calculate middle position
            middle_pos = self.size // 2
            temp = self.head
            
            # Traverse to the middle position
            for i in range(middle_pos):
                prev = temp
                temp = temp.next
            
            # Insert the new node
            prev.next = new_node
            new_node.next = temp
        
        self.size += 1

    def display(self):
        if self.head is None:
            print("The list is empty")
            return
        
        current = self.head
        print(current.data)
        while current.next != self.head:
            current = current.next
            print(current.data)

# Create and test the circular linked list
cll = CircularLinkedList()

print("Adding nodes to the list...")
cll.add_data(21)
cll.add_data(54)
cll.add_data(78)
cll.add_data(99)

print("Original list:")
cll.display()

print("\nInserting 33 at middle:")
cll.add_in_middle(33)
cll.display()

print("\nInserting 56 at middle:")
cll.add_in_middle(56)
cll.display()

print("\nInserting 0 at middle:")
cll.add_in_middle(0)
cll.display()
Adding nodes to the list...
Original list:
21
54
78
99

Inserting 33 at middle:
21
54
33
78
99

Inserting 56 at middle:
21
54
33
56
78
99

Inserting 0 at middle:
21
54
33
0
56
78
99

How Middle Insertion Works

The add_in_middle() method follows these steps:

  • Calculate Position: Find the middle position using size // 2
  • Traverse: Move to the position just before the middle
  • Insert: Link the new node between the current middle nodes
  • Update Size: Increment the size counter

Key Features

Operation Time Complexity Description
Add at End O(1) Direct insertion using tail pointer
Add at Middle O(n) Requires traversal to middle position
Display O(n) Traverse all nodes once

Conclusion

Inserting at the middle of a circular linked list requires calculating the middle position and traversing to that location. The circular nature is maintained by ensuring the tail always points back to the head, creating an unbroken circle.

Updated on: 2026-03-25T17:48:23+05:30

400 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements