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,
- 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++
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node* next;
Node( int d)
{
data = d;
next = NULL;
}
} ;
Node* push(Node* head, int data)
{
if (!head)
return new Node(data);
Node* temp = new Node(data);
temp->next = head;
head = temp;
return head;
}
int sumOfOddFreqEle(Node* head)
{
map< int , int > mp ;
Node* temp = head;
while (temp)
{
int d = temp->data;
mp[d]++;
temp = temp->next;
}
int total_sum = 0;
for ( auto i : mp)
{
if (i.second % 2 == 1)
total_sum+=(i.second * i.first);
}
return total_sum;
}
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;
}
|
Java
import java.util.*;
class GFG
{
static class Node
{
int data;
Node next;
Node( int d)
{
data = d;
next = null ;
}
};
static Node push(Node head, int data)
{
if (head == null )
return new Node(data);
Node temp = new Node(data);
temp.next = head;
head = temp;
return head;
}
static int sumOfOddFreqEle(Node head)
{
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;
}
int total_sum = 0 ;
for (Map.Entry<Integer,
Integer> i : mp.entrySet())
{
if (i.getValue() % 2 == 1 )
total_sum+=(i.getValue() * i.getKey());
}
return total_sum;
}
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)));
}
}
|
Python
class Node:
def __init__( self , data):
self .data = data
self . next = None
def push(head, data):
if not head:
return Node(data)
temp = Node(data)
temp. next = head
head = temp
return head
def sumOfOddFreqEle(head):
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
total_sum = 0
for digit in mp:
tms = mp.get(digit)
if tms % 2 = = 1 :
total_sum + = (tms * digit)
return total_sum
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#
using System;
using System.Collections.Generic;
class GFG
{
public class Node
{
public int data;
public Node next;
public Node( int d)
{
data = d;
next = null ;
}
};
static Node push(Node head, int data)
{
if (head == null )
return new Node(data);
Node temp = new Node(data);
temp.next = head;
head = temp;
return head;
}
static int sumOfOddFreqEle(Node head)
{
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;
}
int total_sum = 0;
foreach (KeyValuePair< int , int > i in mp)
{
if (i.Value % 2 == 1)
total_sum += (i.Value * i.Key);
}
return total_sum;
}
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)));
}
}
|
Javascript
<script>
class Node {
constructor(d) {
this .data = d;
this .next = null ;
}
}
function push(head, data) {
if (head == null ) return new Node(data);
var temp = new Node(data);
temp.next = head;
head = temp;
return head;
}
function sumOfOddFreqEle(head) {
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;
}
var total_sum = 0;
for (const [key, value] of Object.entries(mp)) {
if (value % 2 == 1) total_sum += value * key;
}
return total_sum;
}
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>
|
Time Complexity: O(N*logN)
Auxiliary Space: O(N)
Last Updated :
19 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...