Python program to delete a node from the middle of the Circular Linked List

When it is required to delete a node from 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.

Another class needs to be created that would have an initialization function, and the head of the node would be initialized to None. The size variable is initialized to 0.

There will be user defined functions that help add node to the linked list, print them on the console, and delete a node from the middle index.

Example

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, my_data):
        new_node = Node(my_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 delete_from_mid(self):
        if self.head is None:
            return
        
        # Calculate middle position
        count = self.size // 2 if self.size % 2 == 0 else (self.size + 1) // 2
        
        if self.head != self.tail:
            temp = self.head
            curr = None
            for i in range(count - 1):
                curr = temp
                temp = temp.next
            
            if curr is not None:
                curr.next = temp.next
                if temp == self.tail:
                    self.tail = curr
            else:
                # Deleting the head node
                self.tail.next = temp.next
                self.head = temp.next
        else:
            # Only one node in the list
            self.head = None
            self.tail = None
        
        self.size -= 1

    def print_it(self):
        if self.head is None:
            print("The list is empty")
            return
        
        curr = self.head
        result = []
        while True:
            result.append(str(curr.data))
            curr = curr.next
            if curr == self.head:
                break
        print(" ".join(result))

# Create circular linked list and demonstrate deletion from middle
my_cl = CircularLinkedList()
my_cl.add_data(11)
my_cl.add_data(52)
my_cl.add_data(36)
my_cl.add_data(74)

print("The original list is:")
my_cl.print_it()

while my_cl.head is not None:
    my_cl.delete_from_mid()
    print("The list after updation is:")
    my_cl.print_it()

Output

The original list is:
11 52 36 74
The list after updation is:
11 36 74
The list after updation is:
11 74
The list after updation is:
74
The list after updation is:
The list is empty

How It Works

The middle node deletion algorithm follows these steps:

  • Calculate the middle position: count = size // 2 for even sizes, (size + 1) // 2 for odd sizes
  • Traverse to the node just before the middle node
  • Update the next pointer to skip the middle node
  • Handle special cases like single node or deleting the head/tail
  • Decrement the size counter

Key Points

  • The Node class contains data and a pointer to the next node
  • The CircularLinkedList class manages the head, tail, and size
  • The delete_from_mid() method calculates the middle position and removes that node
  • Special handling is required for edge cases like empty lists or single-node lists
  • The circular nature means the tail always points back to the head

Conclusion

Deleting from the middle of a circular linked list requires careful pointer manipulation and handling of edge cases. The algorithm efficiently finds the middle position and updates the links to maintain the circular structure.

Updated on: 2026-03-25T17:46:55+05:30

278 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements