Sum of all odd frequency nodes of the Linked List

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) = 30

Input: 6 -> 2 -> 2 -> 6 -> 2 -> 1 -> NULL
Output: 7

Approach: This problem can be solved by hashing,

  1. Create a hash to store the frequencies of the nodes.
  2. Traverse the Linked List and update the frequencies of the nodes in the hash variable.
  3. Now, traverse the linked list again and for every node that appears odd number of times, add it’s value to the running sum.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

30


My Personal Notes arrow_drop_up

Coding Is My First Crush 😉

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : andrew1234