Given a linked list, the task is to find the sum of all the odd frequency nodes from the given linked list.
Input: 8 -> 8 -> 1 -> 4 -> 1 -> 2 -> 8 -> NULL
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
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 it’s value to the running sum.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Splitting starting N nodes into new Circular Linked List while preserving the old nodes
- Append odd position nodes in reverse at the end of even positioned nodes in a Linked List
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Delete N nodes after M nodes of a linked list
- Find the element in a linked list with frequency at least N/3
- Count minimum frequency elements in a linked list
- Linked List Sum of Nodes Between 0s
- Sum of all distinct nodes in a linked list
- Sum of the alternate nodes of linked list
- Sum of the nodes of a Singly Linked List
- Linked List Product of Nodes Between 0s
- Sum of the nodes of a Circular Linked List
- Sum and Product of all the nodes which are less than K in the linked list
- Segregate even and odd nodes in a Linked List
- Find the sum of last n nodes of the given Linked List
- Find sum of even and odd nodes in a linked list
- Print nodes of linked list at given indexes
- Delete all odd nodes of a Circular Linked List
- Sum of nodes in a linked list which are greater than next node
- Rearrange a linked list such that all even and odd positioned nodes are together
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.