Absolute distinct count in a Linked List | C ++ Program to print the count of distinct absolute values in a linked list ; Node of a singly linked list ; Function to insert a node at the beginning of a singly Linked List ; Allocate node ; Insert the data ; Point to the current head ; Make the current Node the new head ; Function to return number of distinct absolute values present in the linked list ; Driver Code ; Create the head ; Insert Nodes
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * next ; } ; void 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 ; } int distinctCount ( Node * head_1 ) { Node * ptr = head_1 ; unordered_set < int > s ; while ( ptr != NULL ) { s . insert ( abs ( ptr -> data ) ) ; ptr = ptr -> next ; } return s . size ( ) ; } int main ( ) { Node * head1 = NULL ; push ( & head1 , -1 ) ; push ( & head1 , -2 ) ; push ( & head1 , 0 ) ; push ( & head1 , 4 ) ; push ( & head1 , 5 ) ; push ( & head1 , 8 ) ; int k = distinctCount ( head1 ) ; cout << k ; return 0 ; }