U

53. Count Nodes and Sum of Linked List Values

Easy

Problem Description

Given a linked list of NN integers, count the total number of nodes and compute the sum of all node values.

Input Format

  • •Line 1: An integer NN (0≤N≤1050 \le N \le 10^5).
  • •Line 2: NN space-separated integers. (Omitted if N=0N = 0).

Output Format

Print two space-separated integers: count sum. If empty, print 0 0.

Example 1:
Input:
4
1 2 3 4
Output:
4 10
Explanation: 4 nodes with sum 1+2+3+4 = 10.
Example 2:
Input:
0
Output:
0 0
Explanation: Empty list has count 0 and sum 0.
Example 3:
Input:
2
-5 5
Output:
2 0
Explanation: Sum of -5 and 5 is 0.
Constraints:
  • Standard integer bounds: 32-bit signed integer or input range [1, 10^5].
  • Time complexity target: O(N) or O(N log N) where applicable.
  • Auxiliary space target: O(1) or minimal auxiliary memory.
Follow up:Can this algorithm be adapted to an online streaming setting where the entire input cannot be held in memory simultaneously?
1,180 Online