...
/Singly Linked List (Implementation)
Singly Linked List (Implementation)
create a linked list, add a node, and add and remove data to a linked list (Reading time: 4 minutes)
We'll cover the following...
In a singly linked list, we should be able to find, remove, and insert nodes. Before we can make a list, we first need to create the nodes.
function Node(data) {this.data = data;this.next = null;}
We now created a constructor function that we can use each time we create a new node. By default, the new node’s next value is null, and its data is equal to the data we pass as an argument.
The linked list will be a class. This class will have several properties, such as the remove function, add function, and find function. However, before we can do any of that, we need to create its constructor.
class SinglyLinkedList {constructor() {this.head = null;this.tail = null;}}
By default, the list doesn’t have any nodes, and the length is equal to 0. If the list doesn’t have any nodes, both the head ...