Member-only story
The following is attempt to understand to explain how to reverse a linked list in the simplest manner. First I will look at a linked list and then I’ll look at how to reverse it.
A linked list is NOT an array. Many people think it is but it is not.
In JavaScript a singly linked list can look like this (taken from an code example by Sarah Chima Atuonwu):
const list = {
head: {
value: 6
next: {
value: 10
next: {
value: 12
next: {
value: 3
next: null
}
}
}
}
}
};
So a linked list is a unidirectional, one dimensional collection of nodes. It can algorithmically be represented this way as well:
head: {value: 6}, next: {value: 10}, next: {value: 12},
next: {value: 3}, next: null
Algorithmically it can also be represented like this:
(6) -> (10) -> (12) -> (3) -> null
Creating a linked list is shown effectively in Sarah Chima Atuonwu’s blog: