- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sum of the alternate nodes of linked list in C++
In this problem, we are given a linked list. Our task is to print the sum of alternate nodes of the linked list.
Linked list is a sequence of data structure which are connected together via links.
Now, let’s get back to the problem. Here, we will add alternate nodes of the linked list. This means we will add nodes are positions 0, 2, 4, 6, …
Let’s take an example to understand the problem,
Input
4 → 12 → 10 → 76 → 9 → 26 → 1
Output
24
Explanation
considering alternate strings − 4 + 10 + 9 + 1 = 24
To solve this problem, we will visit each node one by one and for every nest node. We will add value to the sum. To keep a check on nodes, we will use a flag.
This can be done using iteration or recursion. We will discuss both here,
Example
Iterative approach
#include <iostream> using namespace std; struct Node { int data; struct Node* next; }; void pushNode(struct Node** head_ref, int newData) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = newData; newNode->next = (*head_ref); (*head_ref) = newNode; } int sumAlternateNodeIt(struct Node* head) { bool flag = true; int sum = 0; while (head != NULL){ if (flag) sum += head->data; flag = !flag; head = head->next; } return sum; } int main(){ struct Node* head = NULL; pushNode(&head, 54); pushNode(&head, 12); pushNode(&head, 87); pushNode(&head, 1); pushNode(&head, 99); pushNode(&head, 11); cout<<"The sum of alternate nodes is "<<sumAlternateNodeIt(head); return 0; }
Output
The sum of alternate nodes is 24
Example
Recursive Approach
#include <iostream> using namespace std; struct Node { int data; struct Node* next; }; void pushNode(struct Node** head_ref, int new_data){ struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node; } void sumAlternateNodeRec(struct Node* node, int& sum, bool flag = true){ if (node == NULL) return; if (flag == true) sum += (node->data); sumAlternateNodeRec(node->next, sum, !flag); } int main(){ struct Node* head = NULL; pushNode(&head, 54); pushNode(&head, 12); pushNode(&head, 87); pushNode(&head, 1); pushNode(&head, 99); pushNode(&head, 11); int sum = 0; sumAlternateNodeRec(head, sum, true); cout<<"The sum of alternate nodes is "<<sum; return 0; }
Output
The sum of alternate nodes is 24
- Related Articles
- Delete alternate nodes of a Linked List in C++
- Product of the alternate nodes of linked list
- Print the alternate nodes of linked list (Iterative Method) in C language
- Print alternate nodes of a linked list using recursion in C++
- Sum of the nodes of a Circular Linked List in C++
- Reverse Alternate K Nodes in a Singly Linked List in C++
- Sum of the nodes of a Singly Linked List in C Program
- Alternate Odd and Even Nodes in a Singly Linked List in C++
- Alternate sorting of Linked list in C++
- Sum of smaller elements of nodes in a linked list in C++
- Find sum of even and odd nodes in a linked list in C++
- Python Program to Print the Alternate Nodes in a Linked List using Recursion
- Remove Zero Sum Consecutive Nodes from Linked List in C++
- Python Program to Print the Alternate Nodes in a Linked List without using Recursion
- Delete N nodes after M nodes of a linked list in C++?

Advertisements