const obj = {
next: {
next: {
next: {
next: {
next: {
next: {
next: null
}
}
}
}
}
}
}
const solution = (obj, accumulator, counter) => {
if (obj) {
accumulator.push(obj);
counter++;
solution(obj.next, accumulator, counter);
}
return {
accumulator,
length: accumulator.length,
counter
};
}
console.log(solution(obj, [], 0));