Find sum of even and odd nodes in a linked list

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 = 16

Input: 5 -> 7 -> 8 -> 10 -> 15
Output:
Even Sum = 18
Odd Sum = 27



Approach: Traverse the whole linked list and for each node:-

  1. If the element is even then we add that element to the variable which is holding the sum of even elements.
  2. 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <iostream>
using namespace std;
  
// Represents node of the linked list
struct Node {
    int data;
    Node* next;
};
  
// Function to insert a node at the
// end of the linked list
void 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 lists
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;
    }
  
    cout << "Even Sum = " << even << endl;
    cout << "Odd Sum = " << odd << endl;
}
  
// Driver code
int 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
class GfG 
{
  
// Represents node of the linked list 
static class Node 
    int data; 
    Node next; 
}
static Node root;
  
// Function to insert a node at the 
// end of the linked list 
static 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 lists 
static 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 code 
public 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System;
  
class GfG 
  
// Represents node of the linked list 
public class Node 
    public int data; 
    public Node next; 
static Node root; 
  
// Function to insert a node at the 
// end of the linked list 
static 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 lists 
static 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 code 
public 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 

chevron_right


Output:

Even Sum = 12
Odd Sum = 16


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



Improved By : prerna saini, andrew1234



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.