Given a singly linked list. The task is to find the sum of nodes of the given linked list.

**Examples:**

Input:7->6->8->4->1Output:26 Sum of nodes: 7 + 6 + 8 + 4 + 1 = 26Input:1->7->3->9->11->5Output:36

**Recursive Solution:**

- Call a function by passing the head and variable to store the sum.
- Then recursively call the function by passing the next of current node and sum variable.
- Add the value of the current node to the sum.

- Then recursively call the function by passing the next of current node and sum variable.

Below is the implementation of above approach:

`// C++ implementation to find the sum of ` `// nodes of the Linked List ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `/* A Linked list node */` `struct` `Node { ` ` ` `int` `data; ` ` ` `struct` `Node* next; ` `}; ` ` ` `// function to insert a node at the ` `// beginning of the linked list ` `void` `push(` `struct` `Node** head_ref, ` `int` `new_data) ` `{ ` ` ` `/* allocate node */` ` ` `struct` `Node* new_node = ` `new` `Node; ` ` ` ` ` `/* put in the data */` ` ` `new_node->data = new_data; ` ` ` ` ` `/* link the old list to the new node */` ` ` `new_node->next = (*head_ref); ` ` ` ` ` `/* move the head to point to the new node */` ` ` `(*head_ref) = new_node; ` `} ` ` ` `// function to recursively find the sum of ` `// nodes of the given linked list ` `void` `sumOfNodes(` `struct` `Node* head, ` `int` `* sum) ` `{ ` ` ` `// if head = NULL ` ` ` `if` `(!head) ` ` ` `return` `; ` ` ` ` ` `// recursively traverse the remaining nodes ` ` ` `sumOfNodes(head->next, sum); ` ` ` ` ` `// accumulate sum ` ` ` `*sum = *sum + head->data; ` `} ` ` ` `// utility function to find the sum of nodes ` `int` `sumOfNodesUtil(` `struct` `Node* head) ` `{ ` ` ` ` ` `int` `sum = 0; ` ` ` ` ` `// find the sum of nodes ` ` ` `sumOfNodes(head, &sum); ` ` ` ` ` `// required sum ` ` ` `return` `sum; ` `} ` ` ` `// Driver program to test above ` `int` `main() ` `{ ` ` ` `struct` `Node* head = NULL; ` ` ` ` ` `// create linked list 7->6->8->4->1 ` ` ` `push(&head, 7); ` ` ` `push(&head, 6); ` ` ` `push(&head, 8); ` ` ` `push(&head, 4); ` ` ` `push(&head, 1); ` ` ` ` ` `cout << ` `"Sum of nodes = "` ` ` `<< sumOfNodesUtil(head); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Sum of nodes = 26

**Time Complexity:** O(N) , N is the number of nodes in a linked list.

**Auxiliary Space:** O(N), only if the stack size is considered during recursive calls.

**Iterative Solution:**

- Initialise a pointer
with the head of the linked list and a*ptr*variable with 0.*sum* - Start traversing the linked list using a loop until all the nodes get traversed.
- Add the value of current node to the sum i.e.
.*sum += ptr -> data* - Increment the pointer to the next node of linked list i.e.
.*ptr = ptr ->next*

- Add the value of current node to the sum i.e.
- Return the
.*sum*

Below is the implementation of above approach:

`// C++ implementation to find the sum of ` `// nodes of the Linked List ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `/* A Linked list node */` `struct` `Node { ` ` ` `int` `data; ` ` ` `struct` `Node* next; ` `}; ` ` ` `// function to insert a node at the ` `// beginning of the linked list ` `void` `push(` `struct` `Node** head_ref, ` `int` `new_data) ` `{ ` ` ` `/* allocate node */` ` ` `struct` `Node* new_node = ` `new` `Node; ` ` ` ` ` `/* put in the data */` ` ` `new_node->data = new_data; ` ` ` ` ` `/* link the old list to the new node */` ` ` `new_node->next = (*head_ref); ` ` ` ` ` `/* move the head to point to the new node */` ` ` `(*head_ref) = new_node; ` `} ` ` ` `// function to find the sum of ` `// nodes of the given linked list ` `int` `sumOfNodes(` `struct` `Node* head) ` `{ ` ` ` `struct` `Node* ptr = head; ` ` ` `int` `sum = 0; ` ` ` `while` `(ptr != NULL) { ` ` ` ` ` `sum += ptr->data; ` ` ` `ptr = ptr->next; ` ` ` `} ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Driver program to test above ` `int` `main() ` `{ ` ` ` `struct` `Node* head = NULL; ` ` ` ` ` `// create linked list 7->6->8->4->1 ` ` ` `push(&head, 7); ` ` ` `push(&head, 6); ` ` ` `push(&head, 8); ` ` ` `push(&head, 4); ` ` ` `push(&head, 1); ` ` ` ` ` `cout << ` `"Sum of nodes = "` ` ` `<< sumOfNodes(head); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Sum of nodes = 26

**Time Complexity:** O(N) , N is the number of nodes in a linked list.

**Auxiliary Space:** O(1)

## Recommended Posts:

- Product of the nodes of a Singly Linked List
- Alternate Odd and Even Nodes in a Singly Linked List
- Count of Prime Nodes of a Singly Linked List
- Find the common nodes in two singly linked list
- Reverse alternate K nodes in a Singly Linked List
- Sum and Product of all Prime Nodes of a Singly Linked List
- Delete all Non-Prime Nodes from a Singly Linked List
- Delete all Prime Nodes from a Singly Linked List
- Sum and Product of the nodes of a Singly Linked List which are divisible by K
- Delete all Prime Nodes from a Circular Singly Linked List
- Sum and Product of the nodes of a Circular Singly Linked List which are divisible by K
- Update adjacent nodes if the current node is zero in a Singly Linked List
- Difference between Singly linked list and Doubly linked list
- Convert singly linked list into circular linked list
- Convert Singly Linked List to XOR Linked List

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.