Mean of distinct odd fibonacci nodes in a Linked List | Structure of a singly Linked List ; Stores data value of a Node ; Stores pointer to next Node ; Function to add a node at the beginning of the singly Linked List ; Create a new Node ; Insert the data into the Node ; Insert pointer to the next Node ; Update head_ref ; Function to find the largest element from the linked list ; Stores the largest element in the linked list ; Iterate over the linked list ; If max is less than head . data ; Update max ; Update head ; Function to store all Fibonacci numbers up to the largest element of the list ; Store all Fibonacci numbers up to Max ; Stores first element of Fibonacci number ; Stores second element of Fibonacci number ; Insert prev into hashmap ; Insert curr into hashmap ; Insert all elements of Fibonacci numbers up to Max ; Stores current fibonacci number ; Insert temp into hashmap ; Update prev ; Update curr ; Function to find the mean of odd Fibonacci nodes ; Stores the largest element in the linked list ; Stores all fibonacci numbers up to Max ; Stores current node of linked list ; Stores count of odd Fibonacci nodes ; Stores sum of all odd fibonacci nodes ; Traverse the linked list ; if the data value of current node is an odd number ; if data value of the node is present in hashmap ; Update cnt ; Update sum ; Remove current fibonacci number from hashmap so that duplicate elements can 't be counted ; Update curr ; Return the required mean ; Driver Code ; Stores head node of the linked list ; Insert all data values in the linked list
class Node : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . data = 0 NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def push ( head_ref , new_data ) : NEW_LINE INDENT new_node = Node ( ) NEW_LINE new_node . data = new_data ; NEW_LINE new_node . next = head_ref NEW_LINE head_ref = new_node ; NEW_LINE return head_ref NEW_LINE DEDENT def largestElement ( head_ref ) : NEW_LINE INDENT Max = - 10000000 NEW_LINE head = head_ref ; NEW_LINE while ( head != None ) : NEW_LINE INDENT if ( Max < head . data ) : NEW_LINE INDENT Max = head . data ; NEW_LINE DEDENT head = head . next ; NEW_LINE DEDENT return Max ; NEW_LINE DEDENT def createHashMap ( Max ) : NEW_LINE INDENT hashmap = set ( ) NEW_LINE prev = 0 ; NEW_LINE curr = 1 ; NEW_LINE hashmap . add ( prev ) ; NEW_LINE hashmap . add ( curr ) ; NEW_LINE while ( curr <= Max ) : NEW_LINE INDENT temp = curr + prev ; NEW_LINE hashmap . add ( temp ) ; NEW_LINE prev = curr ; NEW_LINE curr = temp ; NEW_LINE DEDENT return hashmap ; NEW_LINE DEDENT def meanofnodes ( head ) : NEW_LINE INDENT Max = largestElement ( head ) ; NEW_LINE hashmap = createHashMap ( Max ) ; NEW_LINE curr = head ; NEW_LINE cnt = 0 ; NEW_LINE sum = 0.0 ; NEW_LINE while ( curr != None ) : NEW_LINE INDENT if ( ( curr . data ) % 2 == 1 ) : NEW_LINE INDENT if ( curr . data in hashmap ) : NEW_LINE INDENT cnt += 1 NEW_LINE sum += curr . data ; NEW_LINE hashmap . remove ( curr . data ) ; NEW_LINE DEDENT DEDENT curr = curr . next ; NEW_LINE DEDENT return ( sum / cnt ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None ; NEW_LINE head = push ( head , 5 ) ; NEW_LINE head = push ( head , 21 ) ; NEW_LINE head = push ( head , 8 ) ; NEW_LINE head = push ( head , 12 ) ; NEW_LINE head = push ( head , 3 ) ; NEW_LINE head = push ( head , 13 ) ; NEW_LINE head = push ( head , 144 ) ; NEW_LINE head = push ( head , 6 ) ; NEW_LINE print ( meanofnodes ( head ) ) NEW_LINE DEDENT