Given a linked list, the task is to find the sum of even and odd nodes in it separately.
Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7
Even Sum = 12
Odd Sum = 16
Input: 5 -> 7 -> 8 -> 10 -> 15
Even Sum = 18
Odd Sum = 27
Approach: Traverse the whole linked list and for each node:-
- If the element is even then we add that element to the variable which is holding the sum of even elements.
- If the element is odd then we add that element to the variable which is holding the sum of odd elements.
Below is the implementation of the above approach:
Even Sum = 12 Odd Sum = 16
- Find the sum of last n nodes of the given Linked List
- Find the product of first k nodes of the given Linked List
- Find the product of last N nodes of the given Linked List
- Find the common nodes in two singly linked list
- Program to find average of all nodes in a Linked List
- 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
- Linked List Sum of Nodes Between 0s
- Sum of all odd frequency nodes of the 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 of the alternate nodes of linked list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.