In this program, you’ll be learning how to remove duplicate elements from the circular list. This program will delete duplicate elements from the circular linked list in Python.
1->2->2->4->3
In the above list there the numbers are not in sequence and also we can see there are 2 two’s.
1->2->3->4
Here are the steps that we’re going to follow while writing the below code.
- First Create a class Node with the instance variables data and next.
- Create 1 more class named LinkedList with the instance a variable head and last_node.
- As this is a circular link list the variable heads point towards the first element in the link list while the last_node points to the last.
- Now we’ll define methods append, get_prev_node, remove and display.
- Then the get_prev_node returns none when the reference node is the first node
Program to remove duplicate elements from a Circular Linked List
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
self.last_node = None
def append(self, data):
if self.last_node is None:
self.head = Node(data)
self.last_node = self.head
else:
self.last_node.next = Node(data)
self.last_node = self.last_node.next
def get_prev_node(self, ref_node):
current = self.head
while (current and current.next != ref_node):
current = current.next
return current
def remove(self, node):
prev_node = self.get_prev_node(node)
if prev_node is None:
self.head = self.head.next
else:
prev_node.next = node.next
def display(self):
current = self.head
while current:
print(current.data, end = ' ')
current = current.next
def remove_duplicates(llist):
current1 = llist.head
while current1:
data = current1.data
current2 = current1.next
while current2:
if current2.data == data:
llist.remove(current2)
current2 = current2.next
current1 = current1.next
a_llist = LinkedList()
data_list = input('Enter Numbers to remove duplicate: ').split()
for data in data_list:
a_llist.append(int(data))
remove_duplicates(a_llist)
print('The final list with duplicates removed: ')
a_llist.display()
#Displays all the nodes in the list
def display(self):
current = self.head;
if self.head is None:
print("List is empty");
return;
else:
#Prints each node by incrementing pointer.
print(current.data);
while(current.next != self.head):
current = current.next;
print(current.data);
print("\n");
class CircularLinkedList:
cl = LinkedList();
#Adds data to the list
cl.add(1);
cl.add(2);
cl.add(3);
cl.add(2);
cl.add(2);
cl.add(4);
print("Originals list: ");
cl.display();
#Removes duplicate nodes
cl.remove_duplicates();
print("List after removing duplicates: ");
cl.display();
Output
Case 1:
Enter Numbers to remove duplicate: 1 5 2 3 7 6 6 7 9
The final list with duplicates removed:
1 2 3 6 7 9
Case 2:
Enter Numbers to remove duplicate: 9 7 4
The final list with duplicates removed:
4 7 9
Case 3:
Enter Numbers to remove duplicate: 1 6 9 7 8 8 9
The final list with duplicates removed:
1 6 7 8 9
Program Explanation
- An instance of LinkedList is created.
- The user is prompted to enter the data items for the list.
- The function remove_duplicates is called to remove duplicates from the list.
- Increment index to index.next and current to current .next.
- The linked list is displayed.
Related Programs
- Python Program to delete a new node from the middle of the doubly linked list.
- Python Program to insert a new node at the beginning of the Circular Linked List.
- Python program to create and display a doubly linked list.
- Python program convert decimal number to binary number using recursion
- Python Program to Find Factorial of a Number Using Recursion
- Python Program to Check if a Number is Positive, Negative or 0
- Python Program to Display Fibonacci Sequence Using Recursion
- Python Program to Find HCF or GCD
- Python Program To Display Powers of 2 Using Anonymous Function
- Python Program to Display Calendar
Ask your questions and clarify your/other doubts on by commenting. Python Documentation