Sum of all odd frequency nodes of the Linked List Last Updated : 19 Aug, 2021 Comments Improve Suggest changes Like Article Like Report Given a linked list, the task is to find the sum of all the odd frequency nodes from the given linked list.Examples: Input: 8 -> 8 -> 1 -> 4 -> 1 -> 2 -> 8 -> NULL Output: 30 freq(8) = 3 freq(1) = 2 freq(4) = 1 freq(2) = 1 8, 4 and 2 appear odd number of times and (8 * 3) + (4 * 1) + (2 * 1) = 30Input: 6 -> 2 -> 2 -> 6 -> 2 -> 1 -> NULL Output: 7 Approach: This problem can be solved by hashing, Create a hash to store the frequencies of the nodes.Traverse the Linked List and update the frequencies of the nodes in the hash variable.Now, traverse the linked list again and for every node that appears odd number of times, add its value to the running sum. Below is the implementation of the above approach: C++ // C++ implementation of above approach #include<bits/stdc++.h> using namespace std; // Node class struct Node { int data; Node* next; Node(int d) { data = d; next = NULL; } } ; // Function to push the new node // to head of the linked list Node* push(Node* head,int data) { // If head is null return new node as head if (!head) return new Node(data); Node* temp =new Node(data); temp->next = head; head = temp; return head; } // Function to find the sum of all odd // frequency nodes of the linked list int sumOfOddFreqEle(Node* head) { // Hash to store the frequencies of // the nodes of the linked list map<int,int> mp ; Node* temp = head; while(temp) { int d = temp->data; mp[d]++; temp = temp->next; } // Initialize total_sum as zero int total_sum = 0; // Traverse through the map to get the sum for (auto i : mp) { // If it appears for odd number of // times then add it to the sum if (i.second % 2 == 1) total_sum+=(i.second * i.first); } return total_sum; } // Driver code int main() { Node* head = NULL; head = push(head, 8); head = push(head, 2); head = push(head, 1); head = push(head, 4); head = push(head, 1); head = push(head, 8); head = push(head, 8); cout<<(sumOfOddFreqEle(head)); return 0; } // This code is contributed by Arnab Kundu Java // Java implementation of the approach import java.util.*; class GFG { // Node class static class Node { int data; Node next; Node(int d) { data = d; next = null; } }; // Function to push the new node // to head of the linked list static Node push(Node head,int data) { // If head is null return new node as head if (head == null) return new Node(data); Node temp = new Node(data); temp.next = head; head = temp; return head; } // Function to find the sum of all odd // frequency nodes of the linked list static int sumOfOddFreqEle(Node head) { // Hash to store the frequencies of // the nodes of the linked list HashMap<Integer, Integer> mp = new HashMap<Integer, Integer>(); Node temp = head; while(temp != null) { int d = temp.data; if(mp.containsKey(d)) { mp.put(d, mp.get(d) + 1); } else { mp.put(d, 1); } temp = temp.next; } // Initialize total_sum as zero int total_sum = 0; // Traverse through the map to get the sum for (Map.Entry<Integer, Integer> i : mp.entrySet()) { // If it appears for odd number of // times then add it to the sum if (i.getValue() % 2 == 1) total_sum+=(i.getValue() * i.getKey()); } return total_sum; } // Driver code public static void main(String[] args) { Node head = null; head = push(head, 8); head = push(head, 2); head = push(head, 1); head = push(head, 4); head = push(head, 1); head = push(head, 8); head = push(head, 8); System.out.println((sumOfOddFreqEle(head))); } } // This code is contributed by Rajput-Ji Python # Python implementation of the approach # Node class class Node: def __init__(self, data): self.data = data self.next = None # Function to push the new node # to head of the linked list def push(head, data): # If head is null return new node as head if not head: return Node(data) temp = Node(data) temp.next = head head = temp return head # Function to find the sum of all odd # frequency nodes of the linked list def sumOfOddFreqEle(head): # Hash to store the frequencies of # the nodes of the linked list mp ={} temp = head while(temp): d = temp.data if d in mp: mp[d]= mp.get(d)+1 else: mp[d]= 1 temp = temp.next # Initialize total_sum as zero total_sum = 0 # Traverse through the map to get the sum for digit in mp: # keep tracking the to tms = mp.get(digit) # If it appears for odd number of # times then add it to the sum if tms % 2 == 1: total_sum+=(tms * digit) return total_sum # Driver code if __name__=='__main__': head = None head = push(head, 8) head = push(head, 2) head = push(head, 1) head = push(head, 4) head = push(head, 1) head = push(head, 8) head = push(head, 8) print(sumOfOddFreqEle(head)) C# // C# implementation of the approach using System; using System.Collections.Generic; class GFG { // Node class public class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } }; // Function to push the new node // to head of the linked list static Node push(Node head,int data) { // If head is null, // return new node as head if (head == null) return new Node(data); Node temp = new Node(data); temp.next = head; head = temp; return head; } // Function to find the sum of all odd // frequency nodes of the linked list static int sumOfOddFreqEle(Node head) { // Hash to store the frequencies of // the nodes of the linked list Dictionary<int, int> mp = new Dictionary<int, int>(); Node temp = head; while(temp != null) { int d = temp.data; if(mp.ContainsKey(d)) { mp[d] = mp[d] + 1; } else { mp.Add(d, 1); } temp = temp.next; } // Initialize total_sum as zero int total_sum = 0; // Traverse through the map to get the sum foreach(KeyValuePair<int, int> i in mp) { // If it appears for odd number of // times then add it to the sum if (i.Value % 2 == 1) total_sum += (i.Value * i.Key); } return total_sum; } // Driver code public static void Main(String[] args) { Node head = null; head = push(head, 8); head = push(head, 2); head = push(head, 1); head = push(head, 4); head = push(head, 1); head = push(head, 8); head = push(head, 8); Console.WriteLine((sumOfOddFreqEle(head))); } } // This code is contributed by Rajput-Ji JavaScript <script> // JavaScript implementation of the approach // Node class class Node { constructor(d) { this.data = d; this.next = null; } } // Function to push the new node // to head of the linked list function push(head, data) { // If head is null, // return new node as head if (head == null) return new Node(data); var temp = new Node(data); temp.next = head; head = temp; return head; } // Function to find the sum of all odd // frequency nodes of the linked list function sumOfOddFreqEle(head) { // Hash to store the frequencies of // the nodes of the linked list var mp = {}; var temp = head; while (temp != null) { var d = temp.data; if (mp.hasOwnProperty(d)) { mp[d] = mp[d] + 1; } else { mp[d] = 1; } temp = temp.next; } // Initialize total_sum as zero var total_sum = 0; // Traverse through the map to get the sum for (const [key, value] of Object.entries(mp)) { // If it appears for odd number of // times then add it to the sum if (value % 2 == 1) total_sum += value * key; } return total_sum; } // Driver code var head = null; head = push(head, 8); head = push(head, 2); head = push(head, 1); head = push(head, 4); head = push(head, 1); head = push(head, 8); head = push(head, 8); document.write(sumOfOddFreqEle(head) + "<br>"); </script> Output: 30 Time Complexity: O(N*logN)Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article Sum of all odd frequency nodes of the Linked List V Vikash Kumar 37 Follow Improve Article Tags : Linked List Data Structures DSA Hash frequency-counting +1 More Practice Tags : Data StructuresHashLinked List Similar Reads Sum of the alternate nodes of linked list Given a linked list, the task is to print the sum of the alternate nodes of the linked list. Examples: Input : 1 -> 8 -> 3 -> 10 -> 17 -> 22 -> 29 -> 42 Output : 50 Alternate nodes : 1 -> 3 -> 17 -> 29 Input : 10 -> 17 -> 33 -> 38 -> 73 Output : 116 Alternat 12 min read Sum of the nodes of a Circular Linked List Given a singly Circular linked list. The task is to find the sum of nodes of the given linked list. For the above circular list, sum = 2 + 5 + 7 + 8 + 10 = 32 Examples: Input: 11->2->56->12 Output: Sum of Circular linked list is = 81 Input: 2-> 5 -> 7 -> 8 -> 10 Output: Sum of C 7 min read Sum of the nodes of a Singly Linked List Given a singly linked list. The task is to find the sum of nodes of the given linked list. Task is to do A + B + C + D. Examples: Input: 7->6->8->4->1 Output: 26 Sum of nodes: 7 + 6 + 8 + 4 + 1 = 26 Input: 1->7->3->9->11->5 Output: 36 Recursive Solution: Call a function by 12 min read Find sum of even and odd nodes in a linked list Given a linked list, the task is to find the sum of even and odd nodes in it separately. Examples: Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 Output: Even Sum = 12 Odd Sum = 16 Input: 5 -> 7 -> 8 -> 10 -> 15 Output: Even Sum = 18 Odd Sum = 27 Approach: Traverse the whole li 6 min read Subtraction of the alternate nodes of Linked List Given a linked list. The task is to print the difference between the first odd-positioned node with the sum of all other odd-positioned nodes. Examples: Input : 1 -> 8 -> 3 -> 10 -> 17 -> 22 -> 29 -> 42 Output : -48 Alternate nodes : 1 -> 3 -> 17 -> 29 1 - (3 + 17 + 29) 12 min read Linked List Sum of Nodes Between 0s Given a linked list which contains a series of numbers separated by â0â. Add them and store them in the linked list in-place. Note: There will not be continuous zeros in input. Examples: Input : 1->2->3->0->5->4->0->3->2->0 Output : 6->9->5 Input : 1->2->3-> 8 min read Find the sum of last n nodes of the given Linked List Given a linked list and a number n. Find the sum of the last n nodes of the linked list.Constraints: 0 <= n <= number of nodes in the linked list. Examples: Input : 10->6->8->4->12, n = 2 Output : 16 Sum of last two nodes: 12 + 4 = 16 Input : 15->7->9->5->16->14, n = 15+ min read Sum of all distinct nodes in a linked list Given a linked list and it may consist of duplicate nodes. The task is to find the sum of non-duplicate nodes. Examples: Input: 1 -> 2 -> 1 -> 3 -> 4 -> 3 -> NULL Output: 6 2 and 4 are the only non-duplicate nodes and 2 + 4 = 6. Input: 1 -> 3 -> 1 -> 3 -> 1 -> 3 - 7 min read Delete all odd nodes of a Circular Linked List Prerequisite: Delete all the even nodes of a Circular Linked ListGiven a circular singly linked list containing N nodes, the task is to delete all the odd nodes from the list. Examples: Input: 572->112->21->5->1->6 Output: 572 -> 112 -> 6 Explanation: All the odd valued nodes ha 10 min read Like