They say the best way to learn something is to teach it. In this series of articles, I will be teaching you about different data structures and how to build them using JavaScript classes and methods. First off, we will be taking a look at a Singly Linked List.

What is a Linked List?

A linked list is a data structure that consists of nodes. Each node has a value and a pointer to another node. The linked list will have a head, tail, and length property. In a Singly Linked List, each node has only one connection to the next node. In the diagram below, the head is the node with value 5, the tail is the node with value 1, and the length is 4.

Image for post

Linked Lists versus Arrays

Unlike an array, singly-linked lists do not have indexes, so the only way to search or access data would be to start at the head and traverse through each node. This can be highly inefficient.

However, inserting and removing data from a singly-linked list is efficient because you will not need to set new indexes for each node.

Big O Notation for Singly Linked Lists

  • Insertion — O(1)
  • Removal — O(1) or O(N)
  • Searching — O(N)
  • Access — O(N)

#javascript #programming #developer #web-development

Building a Singly Linked List with JavaScript
1.75 GEEK