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++
// 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;
}
Java
// 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
Python3
# Python3 implementation of the approach
import math
# Represents node of the linked list
class Node:
def __init__(self, data):
self.data = data
self.next = None
# Function to insert a node at the
# end of the linked list
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
# Function to print the sum of even
# and odd nodes of the linked lists
def 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 code
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)
# This code is contributed by AbhiThakur
C#
// 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
JavaScript
<script>
// Javascript implementation of the approach
// Represents node of the linked list
class Node {
constructor() {
this.data = 0;
this.next = null;
}
}
// Function to insert a node at the
// end of the linked list
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 to print the sum of even
// and odd nodes of the linked lists
function 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>
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.
Similar Reads
Segregate even and odd nodes in a Linked List Given a Linked List of integers, The task is to modify the linked list such that all even numbers appear before all the odd numbers in the modified linked list. Also, preserve the order of even and odd numbers.Examples: Input: 17->15->8->12->10->5->4->1->7->6->NULLOutpu
15+ min read
Alternate Odd and Even Nodes in a Singly Linked List Given a singly linked list, rearrange the list so that even and odd nodes are alternate in the list.There are two possible forms of this rearrangement. If the first data is odd, then the second node must be even. The third node must be odd and so on. Notice that another arrangement is possible where
15+ min read
Find the balanced node in a Linked List Given a linked list, the task is to find the balanced node in a linked list. A balanced node is a node where the sum of all the nodes on its left is equal to the sum of all the node on its right, if no such node is found then print -1. Examples: Input: 1 -> 2 -> 7 -> 10 -> 1 -> 6 -
8 min read
Sum of all odd frequency nodes of the Linked List Given a linked list, the task is to find the sum of all the odd frequency nodes from the given linked list.Examples: Input: 8 -> 8 -> 1 -> 4 -> 1 -> 2 -> 8 -> NULL Output: 30 freq(8) = 3 freq(1) = 2 freq(4) = 1 freq(2) = 1 8, 4 and 2 appear odd number of times and (8 * 3) + (4 *
7 min read
Find first node of loop in a linked list Given the head of a linked list that may contain a loop. A loop means that the last node of the linked list is connected back to a node in the same list. The task is to find the Starting node of the loop in the linked list if there is no loop in the linked list return -1.Example: Input: Output: 3Exp
14 min read
Reverse the order of all nodes at even position in given Linked List Given a linked list A[] of N integers, the task is to reverse the order of all integers at an even position. Examples: Input: A[] = 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULLOutput: 1 6 3 4 5 2Explanation: Nodes at even position in the given linked list are 2, 4 and 6. So, after reversing
10 min read