Count duplicates in a given linked list | Representation of a node ; Function to insert a node at the beginning ; Function to count the number of duplicate nodes in the linked list ; Starting from the next node ; If some duplicate node is found ; Return the count of duplicate nodes ; Driver Code
class Node { constructor ( ) { var data ; var next ; } } function insert ( head , item ) { var temp = new Node ( ) ; temp . data = item ; temp . next = head ; head = temp ; return head ; } function countNode ( head ) { let count = 0 ; while ( head . next != null ) { let ptr = head . next ; while ( ptr != null ) { if ( head . data == ptr . data ) { count ++ ; break ; } ptr = ptr . next ; } head = head . next ; } 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 ) ) ;