An Introduction to the Linked List Data Structure Using Javascript

Photo by Karine Avetisyan on Unsplash

Introduction

Basics of an Array

numbers = [6, 2, 0, 1, 3];
numbers = [6, 2, 0, 1, 3];
numbers[2] // 0
A diagram of a simple array
numbers = [6, 2, 0, 1, 3];
numbers.unshift(1);
numbers // [1, 6, 2, 0, 1, 3];
A simple use of the .unshift() method.

Linked List: What is it?

A basic Linked List

The Node

node1.next = node2; // node1 -> node2node2.next = node3; // node1 -> node2 -> node3node3.next = node4; // node1 -> node2 -> node3 -> node4node4.next = node5; // node1 -> node2 -> node3 -> node4 -> node5

The List

LinkedList {
head: Node { data: 6, next: Node { data: 2, next: [Node] } }
}
Creating a Linked List

Class Method 1: Add a Node to the Start

LinkedList {
head: Node { data: 8, next: Node { data: 6, next: [Node] } }
}
Using the unshift method on the linked list.

Class Method 2: Add a Node to the End

Node { data: 3, next: Node { data: 8, next: null } }
Using the push method on the linked list.

Class Method 3: Add a Node to a Specific Index

Node {
data: 2,
next: Node { data: 8, next: Node { data: 0, next: [Node] } }
}
Using the insert method on the linked list.

Conclusion: Why Use a Linked List?

Full Stack Web Development Student at Lighthouse Labs