728x90
let list = {
value: 1,
next: {
value: 2,
next: {
value: 3,
next: {
value: 4,
next: null
}
}
}
};
function printListReverseLoop(list) {
let cur = list;
const arr = [];
while(cur) {
arr.push(cur.value);
cur = cur.next;
}
for(let i = arr.length - 1; i >= 0; i--) {
console.log(arr[i]);
}
}
function printListReverseRecursion(list) {
if (list.next) {
printListReverseRecursion(list.next);
}
console.log(list.value);
}
printListReverseLoop(list);
printListReverseRecursion(list);

+ Recent posts