318. Find Sum of All Nodes in Linked List

EasyLinked ListLinked ListTraversalMath

Given the head of a singly linked list, return the sum of all node values.

Traverse the list and accumulate the sum.

Input: Head of a singly linked list.

Output: Integer — sum of all node values.

Examples

Example 1
Input: [1,2,3,4,5]
Output: 15
Explanation: 1+2+3+4+5 = 15.
Example 2
Input: [10,20,30]
Output: 60
Explanation: 10+20+30 = 60.
Example 3
Input: [-3,5,-2]
Output: 0
Explanation: -3+5+(-2) = 0.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →