
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find sum of even and odd nodes in a linked list in C++
In this problem, we are given a linked list. Our task is to find the sum of even and odd nodes in a linked list.
Let's take an example to understand the problem,
Input : linked list : 3 -> 2 -> 5 -> 7 -> 1 -> 9 Output : evenSum = 2 ; oddSum = 25
Explanation −
evenSum = 2 oddSum = 3 + 5 + 7 + 1 + 9 = 25
Solution Approach
A simple approach to solve the problem is traversing the linked list and checking for even or odd values and adding them to their respective sum value.
Algorithm
Step 1 − Traverse the linked list.
Step 1.1 − If the value of the current node is even, add it to evenSum.
Step 1.2 − If the value of the current node is odd, add it to oddSum.
Step 2 − Return oddSum and evenSum.
Example
Program to illustrate the working of our solution
#include <iostream> using namespace std; struct Node { int data; Node* next; }; void insertNode(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; } } bool isEven(int a){ return (a % 2); } void findEvenAndOddSum(Node* root) { int oddSum = 0, evenSum = 0; Node* node = root; while (node != NULL) { if (isEven(node->data)) evenSum += node->data; else oddSum += node->data; node = node->next; } cout<<"Sum of nodes with even value is "<<evenSum<<endl; cout<<"Sum of nodes with odd value is "<<oddSum; } int main() { Node* root = NULL; insertNode(&root, 3); insertNode(&root, 2); insertNode(&root, 5); insertNode(&root, 7); insertNode(&root, 1); insertNode(&root, 9); insertNode(&root, 6); findEvenAndOddSum(root); return 0; }
Output
Sum of nodes with even value is 25 Sum of nodes with odd value is 8
- Related Articles
- Alternate Odd and Even Nodes in a Singly Linked List in C++
- Odd Even Linked List in Python
- Sum of the nodes of a Circular Linked List in C++
- Sum of smaller elements of nodes in a linked list in C++
- Sum of the alternate nodes of linked list in C++
- Delete all the even nodes from a Doubly Linked List in C++
- Sum of the nodes of a Singly Linked List in C Program
- Remove Zero Sum Consecutive Nodes from Linked List in C++
- Check whether the length of given linked list is Even or Odd in Python
- Python program to find the sum of all even and odd digits of an integer list
- Delete N nodes after M nodes of a linked list in C++?
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes in Python
- Delete alternate nodes of a Linked List in C++
- Find the sum of digits of a number at even and odd places in C++
- Arrange consonants and vowels nodes in a linked list in C++?

Advertisements