Why is my count variable not producing the right result?
Expected Output: 15
Actual Output: 0
class Node {
constructor(val) {
this.val = val;
this.next = null;
}
}
const a = new Node(2);
const b = new Node(8);
const c = new Node(3);
const d = new Node(-1);
const e = new Node(7);
a.next = b;
b.next = c;
c.next = d;
d.next = e;
const head = a
const sumList = (head) => {
let count = 0
sum(head, count)
return count
}
const sum = (head, count) => {
if (head === null) return null
count += head.val
sum(head.next, count)
}
console.log(sumList(head));
Input is a linked list providing the head of the list -> "1"