Count duplicates in a given linked list | Java implementation of the approach ; Representation of 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 GFG { static class Node { int data ; Node next ; } ; static Node insert ( Node head , int item ) { Node temp = new Node ( ) ; temp . data = item ; temp . next = head ; head = temp ; return head ; } static int countNode ( Node head ) { int count = 0 ; while ( head . next != null ) { Node ptr = head . next ; while ( ptr != null ) { if ( head . data == ptr . data ) { count ++ ; break ; } ptr = ptr . next ; } head = head . next ; } return count ; } public static void main ( String args [ ] ) { Node head = null ; head = insert ( head , 5 ) ; head = insert ( head , 7 ) ; head = insert ( head , 5 ) ; head = insert ( head , 1 ) ; head = insert ( head , 7 ) ; System . out . println ( countNode ( head ) ) ; } }