Count duplicates in a given linked list | Node of a linked list ; Function to insert a node at the beginning ; Function to count the number of duplicate nodes in the linked list ; Create a hash table insert head ; Traverse through remaining nodes ; Return the count of duplicate nodes ; Driver code
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 head = None NEW_LINE def insert ( ref_head , item ) : NEW_LINE INDENT global head NEW_LINE temp = Node ( ) NEW_LINE temp . data = item NEW_LINE temp . next = ref_head NEW_LINE head = temp NEW_LINE DEDENT def countNode ( head ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT s = set ( ) NEW_LINE s . add ( head . data ) NEW_LINE count = 0 NEW_LINE curr = head . next NEW_LINE while ( curr != None ) : NEW_LINE INDENT if ( curr . data in s ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT s . add ( curr . data ) NEW_LINE curr = curr . next NEW_LINE DEDENT return count NEW_LINE DEDENT insert ( head , 5 ) NEW_LINE insert ( head , 7 ) NEW_LINE insert ( head , 5 ) NEW_LINE insert ( head , 1 ) NEW_LINE insert ( head , 7 ) NEW_LINE print ( countNode ( head ) ) NEW_LINE