Remove duplicates from an unsorted doubly linked list | Node of a 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 DLL is empty or if it contains only a single node ; pick elements one by one ; Compare the picked element with the rest of the elements ; if duplicate , then delete it ; store pointer to the node next to 'ptr2   ; delete node pointed to by 'ptr2   ; update 'ptr2   ; else simply 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 , data = None , next = None ) : NEW_LINE INDENT self . next = next NEW_LINE self . data = data NEW_LINE DEDENT DEDENT def deleteNode ( head_ref , del_ ) : NEW_LINE INDENT if ( head_ref == None or del_ == None ) : NEW_LINE INDENT return head_ref 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 or ( head_ref ) . next == None ) : NEW_LINE INDENT return head_ref NEW_LINE DEDENT ptr1 = head_ref NEW_LINE ptr2 = None NEW_LINE while ( ptr1 != None ) : NEW_LINE INDENT ptr2 = ptr1 . next NEW_LINE while ( ptr2 != None ) : NEW_LINE INDENT if ( ptr1 . data == ptr2 . data ) : NEW_LINE INDENT next = ptr2 . next NEW_LINE head_ref = deleteNode ( head_ref , ptr2 ) NEW_LINE ptr2 = next NEW_LINE DEDENT else : NEW_LINE INDENT ptr2 = ptr2 . next NEW_LINE DEDENT DEDENT ptr1 = ptr1 . next NEW_LINE 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