Find sum of even and odd nodes in a linked list
Last Updated :
12 Sep, 2022
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:-
- 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++
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
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;
}
}
void evenOdd(Node* root)
{
int odd = 0, even = 0;
Node* ptr = root;
while (ptr != NULL) {
if (ptr->data % 2 == 0)
even += ptr->data;
else
odd += ptr->data;
ptr = ptr->next;
}
cout << "Even Sum = " << even << endl;
cout << "Odd Sum = " << odd << endl;
}
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;
}
|
Java
class GfG
{
static class Node
{
int data;
Node next;
}
static Node root;
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;
}
}
static void evenOdd(Node root)
{
int odd = 0 , even = 0 ;
Node ptr = root;
while (ptr != null )
{
if (ptr.data % 2 == 0 )
even += ptr.data;
else
odd += ptr.data;
ptr = ptr.next;
}
System.out.println( "Even Sum = " + even);
System.out.println( "Odd Sum = " + odd);
}
public static void main(String[] args)
{
insert( 1 );
insert( 2 );
insert( 3 );
insert( 4 );
insert( 5 );
insert( 6 );
insert( 7 );
evenOdd(root);
}
}
|
Python3
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
def 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
def evenOdd(root):
odd = 0
even = 0
ptr = root
while (ptr ! = None ):
if (ptr.data % 2 = = 0 ):
even = even + ptr.data
else :
odd = odd + ptr.data
ptr = ptr. next
print ( "Even Sum = " , even)
print ( "Odd Sum = " , odd)
if __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)
|
C#
using System;
class GfG
{
public class Node
{
public int data;
public Node next;
}
static Node root;
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;
}
}
static void evenOdd(Node root)
{
int odd = 0, even = 0;
Node ptr = root;
while (ptr != null )
{
if (ptr.data % 2 == 0)
even += ptr.data;
else
odd += ptr.data;
ptr = ptr.next;
}
Console.WriteLine( "Even Sum = " + even);
Console.WriteLine( "Odd Sum = " + odd);
}
public static void Main(String []args)
{
insert( 1);
insert( 2);
insert( 3);
insert( 4);
insert(5);
insert(6);
insert( 7);
evenOdd(root);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function 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 evenOdd( root)
{
let odd = 0, even = 0;
let ptr = root;
while (ptr != null )
{
if (ptr.data % 2 == 0)
even += ptr.data;
else
odd += ptr.data;
ptr = ptr.next;
}
document.write( "Even Sum = " + even);
document.write( "</br>" );
document.write( "Odd Sum = " + odd);
}
var root = null ;
insert( 1);
insert( 2);
insert( 3);
insert( 4);
insert(5);
insert(6);
insert( 7);
evenOdd(root);
</script>
|
Output:
Even Sum = 12
Odd Sum = 16
Time complexity: O(N) where N is number of nodes in the given linked list.
Auxiliary space: O(1), as constant space is used.