Maximum occurring character in a linked list | Java program to count the maximum occurring character in linked list ; Link list node ; counting the frequency of current element p . data ; if current counting is greater than max ; Push a node to linked list . Note that this function changes the head ; Driver code ; Start with the empty list ; this will create a linked list of character " geeksforgeeks "
import java . util . * ; class GFG { static class Node { char data ; Node next ; } ; static Node head_ref ; static char maxChar ( Node head ) { Node p = head ; int max = - 1 ; char res = '0' ; while ( p != null ) { Node q = p . next ; int count = 1 ; while ( q != null ) { if ( p . data == q . data ) count ++ ; q = q . next ; } if ( max < count ) { res = p . data ; max = count ; } p = p . next ; } return res ; } static void push ( char new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; } public static void main ( String [ ] args ) { head_ref = null ; String str = " skeegforskeeg " ; char st [ ] = str . toCharArray ( ) ; int i ; for ( i = 0 ; i < st . length ; i ++ ) push ( st [ i ] ) ; System . out . print ( maxChar ( head_ref ) ) ; } }