HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

5) Canvas Lesson

How to Create a Basic JavaScript Linked List

6 min to complete · By Ian Currie

In this lesson, you will practice creating classes by implementing a very basic version of the linked list, a classic data structure that often comes up in interviews.

What Is a Linked List

In many modern languages, arrays are one of the most commonly used data structures. Many are tempted into thinking that it's quite simple. It's just a list, after all, and you use it all the time. It's one of the first things you learn about. The truth is that implementing a list that has all the functionality of a JavaScript array, can actually be quite complicated.

If you need to insert, change, and remove items, things get tricky. If you were working in C, whenever you initialize an array you need to allocate enough memory for it and however many things you will add to it in the future. Often this means allocating more memory than you expect to need, just in case. Maybe you want dynamically resizing arrays, that will create a new, larger array if it goes close to capacity. JavaScript does all this for you!

In any case, a linked list is a particular implementation of a list. In the context of a language like C, it's a way to define a list without having to worry about allocating enough memory because it can grow indefinitely.

How to Create a Linked List

In a nutshell, the following is the essence of what makes up a linked list:

class LinkedListNode {
	constructor(value) {
		this.value = value;
		this.next = null;
	}
}

The basis of a linked list node is that it has two attributes:

  1. Its own value
  2. A reference to the next node

How to Navigate a Linked List

This allows you to create chains of values:

const head = new LinkedListNode(1);
head.next = new LinkedListNode(2);
head.next.next = new LinkedListNode(3);
head.next.next.next = new LinkedListNode(4);
head.next.next.next.next = new LinkedListNode(5);
head.next.next.next.next.next = new LinkedListNode(6);

With a constructed linked list, you can now go through the list with a while loop:

let current = head;
console.log(current.value);

while (current.next) {
	console.log(current.next.value);
	current = current.next;
}

console.log('End of linked list');

This code:

  1. Sets a current variable that is set to the first node
  2. Logs the value of current
  3. Starts a while loop that will continue until the value of current.next coerces to false
  4. Each iteration of the while loop will log the value of the .next node
  5. Then set current to the .next node
  6. Once current.next is equal to null, that will mean the end of the list and the while loop will exit

This base linked list class can then optionally be wrapped in another class that manages the whole chain, the head of the linked list, the length, and the last element, and can have a method for adding an element at a certain index. Part of the labs for this chapter will involve expanding this linked list in this way!

Learn by Doing

Copy the examples from this lesson and get them working in your labs. Make a few variations, too.

If you're doing this course step-by-step, and especially if you're in the mentorship program, please be sure to push your work to GitHub when you're done.

Summary: How to Create a JavaScript Linked List

  • A linked list is a classic data structure
  • A linked list is a particular implementations of a list
  • The essence of a linked list is that it has two attributes:
    1. Its own value
    2. A reference to the next node
  • You can navigate a linked list by iterating over it with a loop