Published
Edited
Jul 11, 2022
Importers
Insert cell
Insert cell

// User defined class node
class Node {
// constructor
constructor(element)
{
this.element = element;
this.next = null
}
}
Insert cell
class LinkedList {
constructor()
{
this.head = null;
this.size = 0;
}
// adds an element at the end
// of list
insert(element)
{
// creates a new node
let node = new Node(element);

// to store current node
var current;
// if list is Empty add the
// element and make it head
if (this.head == null) {
this.head = node;
} else {
current = this.head;
// iterate to the end of the
// list
while (current.next) {
if (current.element === element) {
throw `This key already exists: ${element}`
}
current = current.next;
}
// add node
current.next = node;
}
this.size++;
}
// removes a given element from the
// list
removeElement(element)
{
var current = this.head;
var prev = null;
// iterate over the list
while (current != null) {
// comparing element with current
// element if found then remove the
// and return true
if (current.element === element) {
if (prev == null) {
this.head = current.next;
} else {
prev.next = current.next;
}
this.size--;
return current.element;
}
prev = current;
current = current.next;
}
throw `This key does not exist: ${element}`;
}
}
Insert cell
Insert cell
{
let l1 = new LinkedList();
["a", "d", "t", "b"].forEach((element) => {
l1.insert(element);
})
return l1;
}
Insert cell
Insert cell
{
let l1 = new LinkedList();
["a", "d", "t", "b"].forEach((element) => {
l1.insert(element);
})
l1.removeElement("t");
return l1;
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more