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
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.
- Append odd position nodes in reverse at the end of even positioned nodes in a Linked List
- Segregate even and odd nodes in a Linked List
- Rearrange a linked list such that all even and odd positioned nodes are together
- Alternate Odd and Even Nodes in a Singly Linked List
- Segregate even and odd nodes in a Linked List using Deque
- Delete all odd or even positioned nodes from Circular Linked List
- Sum and Product of all even digit sum Nodes of a Singly Linked List
- Sum and Product of nodes with value as even digit sum in Circular Linked List
- Merge odd and even positioned nodes of two Linked Lists alternately
- Sum of all odd frequency nodes of the Linked List
- Remove all the Even Digit Sum Nodes from a Doubly Linked List
- Remove all the Even Digit Sum Nodes from a Circular Singly Linked List
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Remove all even parity nodes from a Doubly and Circular Singly Linked List
- Check whether the length of given linked list is Even or Odd
- Delete all odd nodes of a Circular Linked List
- Delete all the even nodes from a Doubly Linked List
- Replace even nodes of a doubly linked list with the elements of array
- Delete all the even nodes of a Circular Linked List
- Program to delete all even nodes from a Singly 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.