en.javascript.info/1-js/06-advanced-functions/01-recursion/05-output-single-linked-list-reverse/solution.md
Alexey Pyltsyn c6620f6b9d
Fix typo
2019-06-13 11:49:20 +03:00

74 lines
1.4 KiB
Markdown

# Using a recursion
The recursive logic is a little bit tricky here.
We need to first output the rest of the list and *then* output the current one:
```js run
let list = {
value: 1,
next: {
value: 2,
next: {
value: 3,
next: {
value: 4,
next: null
}
}
}
};
function printReverseList(list) {
if (list.next) {
printReverseList(list.next);
}
alert(list.value);
}
printReverseList(list);
```
# Using a loop
The loop variant is also a little bit more complicated then the direct output.
There is no way to get the last value in our `list`. We also can't "go back".
So what we can do is to first go through the items in the direct order and remember them in an array, and then output what we remembered in the reverse order:
```js run
let list = {
value: 1,
next: {
value: 2,
next: {
value: 3,
next: {
value: 4,
next: null
}
}
}
};
function printReverseList(list) {
let arr = [];
let tmp = list;
while (tmp) {
arr.push(tmp.value);
tmp = tmp.next;
}
for (let i = arr.length - 1; i >= 0; i--) {
alert( arr[i] );
}
}
printReverseList(list);
```
Please note that the recursive solution actually does exactly the same: it follows the list, remembers the items in the chain of nested calls (in the execution context stack), and then outputs them.