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
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- 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
- Program to find average of all nodes in a Linked List
- Find the common nodes in two singly linked list
- Find the string formed by joining k consecutive nodes of 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
- Linked List Product of Nodes Between 0s
- Sum of all odd frequency nodes of the Linked List
- Sum and Product of all the nodes which are less than K in the linked list
- Sum of all distinct nodes in a 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.