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:
- 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
- Count minimum frequency elements in a linked list
- Delete N nodes after M nodes of a linked list
- Linked List Sum of Nodes Between 0s
- Sum of the nodes of a Circular Linked List
- Sum of all distinct nodes in a linked list
- Linked List Product of Nodes Between 0s
- Sum of the alternate nodes of 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
- Sum of the nodes of a Singly Linked List
- Segregate even and odd nodes in a 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 using Deque
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.
Improved By : andrew1234