Absolute distinct count in a Linked List | Java program to calculate the count of distinct absolute values in a linked list ; Node of the singly linked list ; Function to insert a node at the beginning of the singly Linked List ; Allocate node ; Insert the data ; Point the current Node to the current head ; Make the current node as the new head ; Function to return the count of distinct absolute values present in the Linked list ; Driver Code ; Create the head ; Insert nodes to the Linked List
import java . util . * ; class GFG { static class Node { int data ; Node next ; } ; static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = ( head_ref ) ; ( head_ref ) = new_node ; return head_ref ; } static int distinctCount ( Node head_ref1 ) { Set < Integer > s = new HashSet < Integer > ( ) ; Node ptr1 = head_ref1 ; while ( ptr1 != null ) { s . add ( Math . abs ( ptr1 . data ) ) ; ptr1 = ptr1 . next ; } return s . size ( ) ; } public static void main ( String args [ ] ) { Node head1 = null ; head1 = push ( head1 , - 1 ) ; head1 = push ( head1 , - 2 ) ; head1 = push ( head1 , 0 ) ; head1 = push ( head1 , 4 ) ; head1 = push ( head1 , 5 ) ; head1 = push ( head1 , 8 ) ; int ans = distinctCount ( head1 ) ; System . out . println ( ans ) ; } }