Remove duplicates from an unsorted doubly linked list | a node of the doubly linked list ; Function to delete a node in a Doubly Linked List . head_ref -- > pointer to head node pointer . del -- > pointer to node to be deleted . ; base case ; If node to be deleted is head node ; Change next only if node to be deleted is NOT the last node ; Change prev only if node to be deleted is NOT the first node ; function to remove duplicates from an unsorted doubly linked list ; if doubly linked list is empty ; unordered_set ' us ' implemented as hash table ; traverse up to the end of the list ; if current data is seen before ; store pointer to the node next to ' current ' node ; delete the node pointed to by 'current   ; update 'current   ; insert the current data in 'us   ; move to the next node ; Function to insert a node at the beginning of the Doubly Linked List ; allocate node ; put in the data ; since we are adding at the beginning , prev is always None ; link the old list off the new node ; change prev of head node to new node ; move the head to point to the new node ; Function to print nodes in a given doubly linked list ; if list is empty ; Driver Code ; Create the doubly linked list : 8 < -> 4 < -> 4 < -> 6 < -> 4 < -> 8 < -> 4 < -> 10 < -> 12 < -> 12 ; remove duplicate nodes
class Node : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . data = 0 NEW_LINE self . next = None NEW_LINE self . prev = None NEW_LINE DEDENT DEDENT def deleteNode ( head_ref , del_ ) : NEW_LINE INDENT if ( head_ref == None or del_ == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT if ( head_ref == del_ ) : NEW_LINE INDENT head_ref = del_ . next NEW_LINE DEDENT if ( del_ . next != None ) : NEW_LINE INDENT del_ . next . prev = del_ . prev NEW_LINE DEDENT if ( del_ . prev != None ) : NEW_LINE INDENT del_ . prev . next = del_ . next NEW_LINE DEDENT return head_ref NEW_LINE DEDENT def removeDuplicates ( head_ref ) : NEW_LINE INDENT if ( ( head_ref ) == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT us = set ( ) NEW_LINE current = head_ref NEW_LINE next = None NEW_LINE while ( current != None ) : NEW_LINE INDENT if ( ( current . data ) in us ) : NEW_LINE INDENT next = current . next NEW_LINE head_ref = deleteNode ( head_ref , current ) NEW_LINE current = next NEW_LINE DEDENT else : NEW_LINE INDENT us . add ( current . data ) NEW_LINE current = current . next NEW_LINE DEDENT DEDENT return head_ref NEW_LINE DEDENT def push ( head_ref , new_data ) : NEW_LINE INDENT new_node = Node ( ) NEW_LINE new_node . data = new_data NEW_LINE new_node . prev = None NEW_LINE new_node . next = ( head_ref ) NEW_LINE if ( ( head_ref ) != None ) : NEW_LINE INDENT ( head_ref ) . prev = new_node NEW_LINE DEDENT ( head_ref ) = new_node NEW_LINE return head_ref NEW_LINE DEDENT def printList ( head ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT print ( " Doubly ▁ Linked ▁ list ▁ empty " ) NEW_LINE DEDENT while ( head != None ) : NEW_LINE INDENT print ( head . data , end = " ▁ " ) NEW_LINE head = head . next NEW_LINE DEDENT DEDENT head = None NEW_LINE head = push ( head , 12 ) NEW_LINE head = push ( head , 12 ) NEW_LINE head = push ( head , 10 ) NEW_LINE head = push ( head , 4 ) NEW_LINE head = push ( head , 8 ) NEW_LINE head = push ( head , 4 ) NEW_LINE head = push ( head , 6 ) NEW_LINE head = push ( head , 4 ) NEW_LINE head = push ( head , 4 ) NEW_LINE head = push ( head , 8 ) NEW_LINE print ( " Original ▁ Doubly ▁ linked ▁ list : " ) NEW_LINE printList ( head ) NEW_LINE head = removeDuplicates ( head ) NEW_LINE print ( " Doubly linked list after removing duplicates : " ) NEW_LINE printList ( head ) NEW_LINE