Home » Blog » Python Program to remove duplicate elements from a Circular Linked List

Python Program to remove duplicate elements from a Circular Linked List

Python Program to remove duplicate elements from a Circular Linked List

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.


In the above list there the numbers are not in sequence and also we can see there are 2 two’s.


Here are the steps that we’re going to follow while writing the below code.

  1. First Create a class Node with the instance variables data and next.
  2. Create 1 more class named LinkedList with the instance a variable head and last_node.
  3. 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.
  4. Now we’ll define methods append, get_prev_node, remove and display.
  5. 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
            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
            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:
            current2 = current2.next
        current1 = current1.next
a_llist = LinkedList()
data_list = input('Enter Numbers to remove duplicate: ').split()
for data in data_list:
print('The final list with duplicates removed: ')

#Displays all the nodes in the list    
  def display(self):    
    current = self.head;    
    if self.head is None:    
      print("List is empty");    
      #Prints each node by incrementing pointer.    
      while(current.next != self.head):    
        current = current.next;    
class CircularLinkedList:    
  cl = LinkedList();    
  #Adds data to the list    
  print("Originals list: ");    
  #Removes duplicate nodes    
  print("List after removing duplicates: ");    


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

  1. An instance of LinkedList is created.
  2. The user is prompted to enter the data items for the list.
  3. The function remove_duplicates is called to remove duplicates from the list.
  4. Increment index to index.next and current to current .next.
  5. The linked list is displayed.

Related Programs

  1. Python Program to delete a new node from the middle of the doubly linked list.
  2. Python Program to insert a new node at the beginning of the Circular Linked List.
  3. Python program to create and display a doubly linked list.
  4. Python program convert decimal number to binary number using recursion
  5. Python Program to Find Factorial of a Number Using Recursion
  6. Python Program to Check if a Number is Positive, Negative or 0
  7. Python Program to Display Fibonacci Sequence Using Recursion
  8. Python Program to Find HCF or GCD
  9. Python Program To Display Powers of 2 Using Anonymous Function
  10. Python  Program to Display Calendar

Ask your questions and clarify your/other doubts on by commenting. Python Documentation

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.