Member-only story

Reversing a Linked List?!? 🤯

MDN
2 min readOct 16, 2020

--

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.

A linked list “is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes which together represent a sequence.” Wikipedia

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:

--

--

MDN
MDN

Written by MDN

Composer, Designer, Educator

No responses yet

Write a response