Count duplicates in a given linked list | Representation of node ; 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 { constructor ( ) { this . data = 0 ; this . next = null ; } } ; function insert ( head , item ) { var temp = new Node ( ) ; temp . data = item ; temp . next = head ; head = temp ; return head ; } function countNode ( head ) { if ( head == null ) return 0 ; ; var s = new Set ( ) ; s . add ( head . data ) ; var count = 0 ; for ( var curr = head . next ; curr != null ; curr = curr . next ) { if ( s . has ( curr . data ) ) count ++ ; s . add ( curr . data ) ; } return count ; } var head = null ; head = insert ( head , 5 ) ; head = insert ( head , 7 ) ; head = insert ( head , 5 ) ; head = insert ( head , 1 ) ; head = insert ( head , 7 ) ; document . write ( countNode ( head ) ) ;