Find sum of even and odd nodes in a linked list

07 Jun, 2021
Given a linked list, the task is to find the sum of even and odd nodes in it separately.Examples:  
Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 Output: Even Sum = 12 Odd Sum = 16Input: 5 -> 7 -> 8 -> 10 -> 15 Output: 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.
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:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <iostream>using namespace std; // Represents node of the linked liststruct Node {    int data;    Node* next;}; // Function to insert a node at the// end of the linked listvoid insert(Node** root, int item){    Node *ptr = *root, *temp = new Node;    temp->data = item;    temp->next = NULL;     if (*root == NULL)        *root = temp;    else {        while (ptr->next != NULL)            ptr = ptr->next;        ptr->next = temp;    }} // Function to print the sum of even// and odd nodes of the linked listsvoid evenOdd(Node* root){    int odd = 0, even = 0;    Node* ptr = root;    while (ptr != NULL) {         // If current node's data is even        if (ptr->data % 2 == 0)            even += ptr->data;         // If current node's data is odd        else            odd += ptr->data;         // ptr now points to the next node        ptr = ptr->next;    }     cout << "Even Sum = " << even << endl;    cout << "Odd Sum = " << odd << endl;} // Driver codeint main(){    Node* root = NULL;    insert(&root, 1);    insert(&root, 2);    insert(&root, 3);    insert(&root, 4);    insert(&root, 5);    insert(&root, 6);    insert(&root, 7);     evenOdd(root);     return 0;}
// Java implementation of the approachclass GfG{ // Represents node of the linked liststatic class Node{    int data;    Node next;}static Node root; // Function to insert a node at the// end of the linked liststatic void insert(int item){    Node ptr = root, temp = new Node();    temp.data = item;    temp.next = null;     if (root == null)        root = temp;    else    {        while (ptr.next != null)            ptr = ptr.next;        ptr.next = temp;    }} // Function to print the sum of even// and odd nodes of the linked listsstatic void evenOdd(Node root){    int odd = 0, even = 0;    Node ptr = root;    while (ptr != null)    {         // If current node's data is even        if (ptr.data % 2 == 0)            even += ptr.data;         // If current node's data is odd        else            odd += ptr.data;         // ptr now points to the next node        ptr = ptr.next;    }     System.out.println("Even Sum = " + even);    System.out.println("Odd Sum = " + odd);} // Driver codepublic static void main(String[] args){    // Node* root = NULL;    insert( 1);    insert( 2);    insert( 3);    insert( 4);    insert(5);    insert(6);    insert( 7);     evenOdd(root);}} // This code is contributed by Prerna Saini
# Python3 implementation of the approachimport math # Represents node of the linked listclass Node:    def __init__(self, data):        self.data = data        self.next = None # Function to insert a node at the# end of the linked listdef insert(root, item):    ptr = root    temp = Node(item)    temp.data = item    temp.next = None     if (root == None):        root = temp    else:        while (ptr.next != None):            ptr = ptr.next        ptr.next = temp         return root # Function to print the sum of even# and odd nodes of the linked listsdef evenOdd(root):    odd = 0    even = 0    ptr = root    while (ptr != None):         # If current node's data is even        if (ptr.data % 2 == 0):            even = even + ptr.data         # If current node's data is odd        else:            odd = odd + ptr.data         # ptr now points to the next node        ptr = ptr.next         print( "Even Sum = ", even)    print( "Odd Sum = ", odd) # Driver codeif __name__=='__main__':    root = None    root = insert(root, 1)    root = insert(root, 2)    root = insert(root, 3)    root = insert(root, 4)    root = insert(root, 5)    root = insert(root, 6)    root = insert(root, 7)     evenOdd(root) # This code is contributed by AbhiThakur
// C# implementation of the approachusing System; class GfG{ // Represents node of the linked listpublic class Node{    public int data;    public Node next;}static Node root; // Function to insert a node at the// end of the linked liststatic void insert(int item){    Node ptr = root, temp = new Node();    temp.data = item;    temp.next = null;     if (root == null)        root = temp;    else    {        while (ptr.next != null)            ptr = ptr.next;        ptr.next = temp;    }} // Function to print the sum of even// and odd nodes of the linked listsstatic void evenOdd(Node root){    int odd = 0, even = 0;    Node ptr = root;    while (ptr != null)    {         // If current node's data is even        if (ptr.data % 2 == 0)            even += ptr.data;         // If current node's data is odd        else            odd += ptr.data;         // ptr now points to the next node        ptr = ptr.next;    }     Console.WriteLine("Even Sum = " + even);    Console.WriteLine("Odd Sum = " + odd);} // Driver codepublic static void Main(String []args){    // Node* root = NULL;    insert( 1);    insert( 2);    insert( 3);    insert( 4);    insert(5);    insert(6);    insert( 7);     evenOdd(root);}} // This code is contributed by Arnab Kundu
<script> // Javascript implementation of the approach // Represents node of the linked listclass Node {        constructor() {                this.data = 0;                this.next = null;             }        }                  // Function to insert a node at the// end of the linked listfunction insert( item){    var ptr = root, temp = new Node();    temp.data = item;    temp.next = null;     if (root == null)        root = temp;    else    {        while (ptr.next != null)            ptr = ptr.next;        ptr.next = temp;    }} // Function to print the sum of even// and odd nodes of the linked listsfunction evenOdd( root){    let odd = 0, even = 0;    let ptr = root;    while (ptr != null)    {         // If current node's data is even        if (ptr.data % 2 == 0)            even += ptr.data;         // If current node's data is odd        else            odd += ptr.data;         // ptr now points to the next node        ptr = ptr.next;    }     document.write("Even Sum = " + even);    document.write("</br>");    document.write("Odd Sum = " + odd);}  // Driver Code var root = null;insert( 1);insert( 2);insert( 3);insert( 4);insert(5);insert(6);insert( 7); evenOdd(root); // This code is contributed by jana_sayantan.</script>
Even Sum = 12
Odd Sum = 16
 
prerna saini
andrew1234
abhaysingh290895
Akanksha_Rai
jana_sayantan
Traversal
Linked List
Linked List
Traversal
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
LinkedList in Java
Introduction to Data Structures
Doubly Linked List | Set 1 (Introduction and Insertion)
What is Data Structure: Types, Classifications and Applications
Linked List vs Array
Implementing a Linked List in Java using Class
Find Length of a Linked List (Iterative and Recursive)
Queue - Linked List Implementation
Function to check if a singly linked list is palindrome
Write a function to get the intersection point of two Linked Lists