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

When it is required to delete a node from the end of a 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 tail 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 stores data and a reference to the next node ?

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

Circular Linked List Implementation

The main class handles insertion, deletion, and display operations ?

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

class CircularLinkedList:
    def __init__(self):
        self.head = None
        self.tail = None
    
    def add_value(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
    
    def delete_from_end(self):
        if self.head is None:
            return
        
        # Only one node
        if self.head == self.tail:
            self.head = self.tail = None
        else:
            # Find node before tail
            curr = self.head
            while curr.next != self.tail:
                curr = curr.next
            self.tail = curr
            self.tail.next = self.head
    
    def print_list(self):
        if self.head is None:
            print("The list is empty")
            return
        
        curr = self.head
        result = []
        result.append(str(curr.data))
        curr = curr.next
        
        while curr != self.head:
            result.append(str(curr.data))
            curr = curr.next
        
        print(" -> ".join(result) + " -> (back to head)")

# Create and test the circular linked list
my_cl = CircularLinkedList()
my_cl.add_value(11)
my_cl.add_value(32)
my_cl.add_value(43)
my_cl.add_value(57)

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

# Delete nodes one by one from the end
while my_cl.head is not None:
    my_cl.delete_from_end()
    print("\nAfter deletion:")
    my_cl.print_list()
The original list is:
11 -> 32 -> 43 -> 57 -> (back to head)

After deletion:
11 -> 32 -> 43 -> (back to head)

After deletion:
11 -> 32 -> (back to head)

After deletion:
11 -> (back to head)

After deletion:
The list is empty

How the Deletion Works

The delete_from_end() method handles two cases:

  • Single node: Set both head and tail to None
  • Multiple nodes: Find the second-to-last node, make it the new tail, and point it to head

Key Points

  • In a circular linked list, the last node always points to the first node
  • When deleting from the end, we must update the tail pointer
  • The circular property must be maintained after deletion
  • Handle the special case when only one node remains

Conclusion

Deleting from the end of a circular linked list requires finding the second-to-last node and updating the tail pointer. The circular property must be preserved by ensuring the new tail points back to the head.

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

434 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements