Python program to remove duplicate elements from a Doubly Linked List.

Created with Sketch.

Python program to remove duplicate elements from a Doubly Linked List.

In this program, we will create a doubly linked list and remove the duplicate, if present, by traversing through the list.

Python program to remove duplicate elements from a Doubly Linked List

List after removing duplicates:

Python program to remove duplicate elements from a Doubly Linked List

In the above list, node2 is repeated thrice, and node 3 is repeated twice. Current will point to head, and index will point to node next to current. Start traversing the list till a duplicate is found that is when current’s data is equal to index’s data. In the above example, the first duplicate will be found at position 4. Assign index to another node temp. Connect index’s previous node with index’s next node. Delete temp which was pointing to duplicate node. This process will continue till all duplicates are removed.

ALGORITHM:

  1. Define a Node class which represents a node in the list. It will have three properties: data, previous which will point to the previous node and next which will point to the next node.
  2. Define another class for creating a doubly linked list, and it has two nodes: head and tail. Initially, head and tail will point to null.
  3. addNode() will add node to the list:
  • It first checks whether the head is null, then it will insert the node as the head.
  • Both head and tail will point to a newly added node.
  • Head’s previous pointer will point to null and tail’s next pointer will point to null.
  • If the head is not null, the new node will be inserted at the end of the list such that new node’s previous pointer will point to tail.
  • The new node will become the new tail. Tail’s next pointer will point to null.

a. removeDuplicateNode() will remove duplicate nodes from the list.

  • Define a new node current which will initially point to head.
  • Node index will always point to node next to current.
  • Loop through the list until current points to null.
  • Check whether current’s data is equal to index’s data that means index is duplicate of current.
  • Node temp will point to index to store duplicate node.
  • Since, temp is pointing to index, which is a duplicate node, so set temp to null.

a. display() will show all the nodes present in the list.

  • Define a new node ‘current’ that will point to the head.
  • Print current.data till current points to null.
  • Current will point to the next node in the list in each iteration.

PROGRAM:

  1. #Represent a node of doubly linked list  
  2. class Node:
  3.     def __init__(self,data):
  4.         self.data = data;
  5.         self.previous = None;
  6.         self.next = None;
  7. class RemoveDuplicate:
  8.     #Represent the head and tail of the doubly linked list  
  9.     def __init__(self):
  10.         self.head = None;
  11.         self.tail = None;
  12.     #addNode() will add a node to the list  
  13.     def addNode(self, data):
  14.         #Create a new node  
  15.         newNode = Node(data);
  16.         #If list is empty  
  17.         if(self.head == None):
  18.             #Both head and tail will point to newNode  
  19.             self.head = self.tail = newNode;
  20.             #head’s previous will point to None  
  21.             self.head.previous = None;
  22.             #tail’s next will point to None, as it is the last node of the list  
  23.             self.tail.next = None;
  24.         else:
  25.             #newNode will be added after tail such that tail’s next will point to newNode  
  26.             self.tail.next = newNode;
  27.             #newNode’s previous will point to tail  
  28.             newNode.previous = self.tail;
  29.             #newNode will become new tail  
  30.             self.tail = newNode;
  31.             #As it is last node, tail’s next will point to None  
  32.             self.tail.next = None;
  33.     #removeDuplicateNode() will remove duplicate nodes from the list  
  34.     def removeDuplicateNode(self):
  35.         #Checks whether list is empty  
  36.         if(self.head == None):
  37.             return;
  38.         else:
  39.             #Initially, current will point to head node  
  40.             current = self.head;
  41.             while(current != None):
  42.                 #index will point to node next to current  
  43.                 index = current.next
  44.                 while(index != None):
  45.                     if(current.data == index.data):
  46.                         #Store the duplicate node in temp  
  47.                         temp = index;
  48.                         #index’s previous node will point to node next to index thus, removes the duplicate node  
  49.                         index.previous.next = index.next;
  50.                         if(index.next != None):
  51.                             index.next.previous = index.previous;
  52.                         #Delete duplicate node by making temp to None  
  53.                         temp = None;
  54.                     index = index.next;
  55.                 current = current.next;
  56.     #display() will print out the nodes of the list  
  57.     def display(self):
  58.         #Node current will point to head  
  59.         current = self.head;
  60.         if(self.head == None):
  61.             print(“List is empty”);
  62.             return;
  63.         while(current != None):
  64.             #Prints each node by incrementing pointer.  
  65.             print(current.data),
  66.             current = current.next;
  67.         print();
  68. dList = RemoveDuplicate();
  69. #Add nodes to the list  
  70. dList.addNode(1);
  71. dList.addNode(2);
  72. dList.addNode(3);
  73. dList.addNode(2);
  74. dList.addNode(2);
  75. dList.addNode(4);
  76. dList.addNode(5);
  77. dList.addNode(3);
  78. print(“Originals list: “);
  79. dList.display();
  80. #Removes duplicate nodes  
  81. dList.removeDuplicateNode();
  82. print(“List after removing duplicates: “);
  83. dList.display();

Output:

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

Leave a Reply

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