JSFiddle - React, Tailwind, and code Playground
by Eugen Sunic
JavaScript
class Node {
constructor(element) {
this.element = element;
this.next = null
}
}
class LinkedList {
constructor() {
this.head = null;
this.size = 0;
}
add(val) {
if (!this.head) {
this.head = new Node(val);
++this.size;
return;
}
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = new Node(val)
++this.size;
}
(val) {
if (!this.head) {
this.head = new Node(val);
++this.size;
return;
}
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = new Node(val)
++this.size;
}
}
const list = new LinkedList();
list.add(3);
list.add(4);
list.add(5);
console.log(list);