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 remove duplicate elements from a Circular Linked List
When it is required to remove duplicates from 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 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.
Multiple methods are defined by the user to add nodes to the linked list, remove duplicates and print the node values.
Implementation
Here's a complete implementation to remove duplicate elements from a circular linked list ?
class Node:
def __init__(self, data):
self.data = data
self.next = None
class CircularLinkedList:
def __init__(self):
self.head = Node(None)
self.tail = Node(None)
self.head.next = self.tail
self.tail.next = self.head
def add_data(self, my_data):
new_node = Node(my_data)
if self.head.data 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 remove_duplicate_vals(self):
curr = self.head
if self.head is None:
print("The list is empty")
else:
while True:
temp = curr
index_val = curr.next
while index_val != self.head:
if curr.data == index_val.data:
temp.next = index_val.next
else:
temp = index_val
index_val = index_val.next
curr = curr.next
if curr.next == self.head:
break
def print_it(self):
curr = self.head
if self.head is None:
print("The list is empty")
return
else:
print(curr.data)
while curr.next != self.head:
curr = curr.next
print(curr.data)
print()
# Example usage
my_cl = CircularLinkedList()
print("Nodes are being added to the list")
my_cl.add_data(21)
my_cl.add_data(54)
my_cl.add_data(78)
my_cl.add_data(99)
my_cl.add_data(21)
my_cl.add_data(54)
print("The original list is:")
my_cl.print_it()
my_cl.remove_duplicate_vals()
print("The updated list after removing duplicates is:")
my_cl.print_it()
The output of the above code is ?
Nodes are being added to the list The original list is: 21 54 78 99 21 54 The updated list after removing duplicates is: 21 54 78 99
How It Works
The duplicate removal algorithm works as follows:
- For each node in the list, compare its data with all subsequent nodes
- If a duplicate is found, remove it by updating the
nextpointer - Continue until all nodes have been checked
- The algorithm maintains the circular structure throughout the process
Key Points
- The
Nodeclass stores data and a reference to the next node - The
CircularLinkedListclass manages the circular structure - The
remove_duplicate_vals()method uses nested loops to find and remove duplicates - The
print_it()method displays all nodes while respecting the circular structure - Time complexity is O(n²) due to nested loops checking each pair of nodes
Conclusion
This implementation effectively removes duplicate elements from a circular linked list by comparing each node with all subsequent nodes. The circular structure is maintained throughout the process, ensuring the list remains properly connected.
