Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to insert a new node at the end of the Circular Linked List
When it is required to insert a new node at the end 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 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.
Another class needs to be created that would have an initialization function, and the head of the node would be initialized to None.
Implementation
Below is a demonstration of inserting nodes at the end 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
def add_at_end(self, data):
new_node = Node(data)
# If list is empty
if self.head is None:
self.head = new_node
self.tail = new_node
new_node.next = self.head # Point to itself
else:
# Add after tail and update connections
self.tail.next = new_node
self.tail = new_node
self.tail.next = self.head # Maintain circular structure
def print_list(self):
if self.head is None:
print("The list is empty")
return
current = self.head
result = []
# Print first node
result.append(str(current.data))
current = current.next
# Print remaining nodes until we reach head again
while current != self.head:
result.append(str(current.data))
current = current.next
print(" -> ".join(result) + " -> (back to head)")
# Create circular linked list and add nodes
cll = CircularLinkedList()
print("Adding nodes to the end of circular linked list:")
cll.add_at_end(21)
print("After adding 21:")
cll.print_list()
cll.add_at_end(53)
print("\nAfter adding 53:")
cll.print_list()
cll.add_at_end(76)
print("\nAfter adding 76:")
cll.print_list()
Adding nodes to the end of circular linked list: After adding 21: 21 -> (back to head) After adding 53: 21 -> 53 -> (back to head) After adding 76: 21 -> 53 -> 76 -> (back to head)
How It Works
The insertion process follows these steps:
- Empty List: If the list is empty, the new node becomes both head and tail, pointing to itself
- Non-empty List: The current tail's next pointer is updated to point to the new node
- Update Tail: The new node becomes the new tail
- Maintain Circle: The new tail's next pointer is set to point back to the head
Key Features
- The
Nodeclass contains data and a next pointer - The
CircularLinkedListclass maintains head and tail pointers - The
add_at_end()method handles both empty and non-empty list cases - The circular structure is maintained by always pointing the tail to the head
Conclusion
Inserting at the end of a circular linked list requires updating the tail pointer and maintaining the circular connection to the head. This operation has O(1) time complexity when a tail pointer is maintained.
