Q:

Python program to remove duplicate elements from a Circular Linked List

belongs to collection: Python Circular Linked List Programs

0

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->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->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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

PROGRAM:

#Represents the node of list.    
#Represents the node of list.    
class Node:    
  def __init__(self,data):    
    self.data = data;    
    self.next = None;    
    
class CreateList:    
  #Declaring head and tail pointer as null.    
  def __init__(self):    
    self.head = Node(None);    
    self.tail = Node(None);    
    self.head.next = self.tail;    
    self.tail.next = self.head;    
      
  #This function will add the new node at the end of the list.    
  def add(self,data):    
    newNode = Node(data);    
    #Checks if the list is empty.    
    if self.head.data is None:    
      #If list is empty, both head and tail would point to new node.    
      self.head = newNode;    
      self.tail = newNode;    
      newNode.next = self.head;    
    else:    
      #tail will point to new node.    
      self.tail.next = newNode;    
      #New node will become new tail.    
      self.tail = newNode;    
      #Since, it is circular linked list tail will point to head.    
      self.tail.next = self.head;    
      
  #Removes duplicate from the list    
  def removeDuplicate(self):    
    #Current will point to head    
    current = self.head;    
    if(self.head == None):    
      print("List is empty");    
    else:    
      while(True):    
        #Temp will point to previous node of index.    
        temp = current;    
        #Index will point to node next to current    
        index = current.next;    
        while(index != self.head):    
          #If current node is equal to index data    
          if(current.data == index.data):    
            #Here, index node is pointing to the node which is duplicate of current node    
            #Skips the duplicate node by pointing to next node    
            temp.next = index.next;    
          else:    
            #Temp will point to previous node of index.    
            temp = index;    
          index= index.next;    
        current =current.next;    
        if(current.next == self.head):    
          break;    
              
  #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 = CreateList();    
  #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.removeDuplicate();    
  print("List after removing duplicates: ");    
  cl.display();    

 

Output:

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python program to search an element in a Circular ... >>
<< Python program to insert a new node at the middle ...