Sum of Linked List Nodes - aloalgo

Sum of Linked List Nodes

Easy

You are given the head of a singly linked list where each node contains an integer, write a function to calculate the sum of all the values in the list.

An empty list should result in a sum of 0.

Example 1

Input
1 → 2 → 3 → 4 → null
Output
10
Explanation:

The sum of the nodes is 1 + 2 + 3 + 4 = 10.

Example 2

Input
None
Output
0
Explanation:

The input list is empty, so the sum is 0.

Example 3

Input
5 → -7 → null
Output
-2
Explanation:

The sum of the nodes is 5 + (-7) = -2.

Loading...
Input
1 → 2 → 3 → 4 → null
Output
10

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!