Python program to remove duplicate elements from a Circular Linked List

Created with Sketch.

Python program to remove duplicate elements from a Circular Linked List

In this program, we will create a circular linked list and remove duplicate nodes from the list. We will compare a node with the rest of the list and check for the duplicate. If the duplicate is found, delete the duplicate node from the list.

  1. 1->2->2->4->3

In the above list, we can see, node 2 is present twice in the list. So, we will have a node current that will iterate through the list. The index will point to the next node to current. Temp will be pointing to the node previous to index. When a duplicate is found, we delete it by pointing temp.next to index.next. Above list after removing duplicates:

  1. 1->2->4->3

ALGORITHM:

  1. Define a Node class which represents a node in the list. It has two properties data and next which will point to the next node.
  2. Define another class for creating the circular linked list, and it has two nodes: head and tail.
  3. removeDuplicate() will remove duplicate nodes from the list:
  • Node current will point to head and used to traverse through the list.
  • The index will point to the next node to current and temp will point to the previous node to index.
  • We will compare the current.data with the index.data. If the match is found, delete duplicate data by pointing temp’s next to index’s next.
  • Increment index to index.next and current to current .next.
  • Repeat step from c to d till all the duplicates are removed.

PROGRAM:

  1. #Represents the node of list.  
  2. #Represents the node of list.  
  3. class Node:
  4.   def __init__(self,data):
  5.     self.data = data;
  6.     self.next = None;
  7. class CreateList:
  8.   #Declaring head and tail pointer as null.  
  9.   def __init__(self):
  10.     self.head = Node(None);
  11.     self.tail = Node(None);
  12.     self.head.next = self.tail;
  13.     self.tail.next = self.head;
  14.   #This function will add the new node at the end of the list.  
  15.   def add(self,data):
  16.     newNode = Node(data);
  17.     #Checks if the list is empty.  
  18.     if self.head.data is None:
  19.       #If list is empty, both head and tail would point to new node.  
  20.       self.head = newNode;
  21.       self.tail = newNode;
  22.       newNode.next = self.head;
  23.     else:
  24.       #tail will point to new node.  
  25.       self.tail.next = newNode;
  26.       #New node will become new tail.  
  27.       self.tail = newNode;
  28.       #Since, it is circular linked list tail will point to head.  
  29.       self.tail.next = self.head;
  30.   #Removes duplicate from the list  
  31.   def removeDuplicate(self):
  32.     #Current will point to head  
  33.     current = self.head;
  34.     if(self.head == None):
  35.       print(“List is empty”);
  36.     else:
  37.       while(True):
  38.         #Temp will point to previous node of index.  
  39.         temp = current;
  40.         #Index will point to node next to current  
  41.         index = current.next;
  42.         while(index != self.head):
  43.           #If current node is equal to index data  
  44.           if(current.data == index.data):
  45.             #Here, index node is pointing to the node which is duplicate of current node  
  46.             #Skips the duplicate node by pointing to next node  
  47.             temp.next = index.next;
  48.           else:
  49.             #Temp will point to previous node of index.  
  50.             temp = index;
  51.           index= index.next;
  52.         current =current.next;
  53.         if(current.next == self.head):
  54.           break;
  55.   #Displays all the nodes in the list  
  56.   def display(self):
  57.     current = self.head;
  58.     if self.head is None:
  59.       print(“List is empty”);
  60.       return;
  61.     else:
  62.       #Prints each node by incrementing pointer.  
  63.       print(current.data);
  64.       while(current.next != self.head):
  65.         current = current.next;
  66.         print(current.data);
  67.     print(“\n”);
  68. class CircularLinkedList:
  69.   cl = CreateList();
  70.   #Adds data to the list  
  71.   cl.add(1);
  72.   cl.add(2);
  73.   cl.add(3);
  74.   cl.add(2);
  75.   cl.add(2);
  76.   cl.add(4);
  77.   print(“Originals list: “);
  78.   cl.display();
  79.   #Removes duplicate nodes  
  80.   cl.removeDuplicate();
  81.   print(“List after removing duplicates: “);
  82.   cl.display();

Output:

Originals list: 
 1 2 3 2 2 4
List after removing duplicates: 
 1 2 3 4

Leave a Reply

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