Linkedlist
by Pritesh Patel
JavaScript
class Node{
constructor(data,next = null) {
this.data = data;
this.next = next;
}
}
class LinkedList{
constructor() {
this.head = null
}
insertFirst(data) {
this.head = new Node(data, this.head);
}
size() {
let counter = 0;
let node = this.head;
while(node){
node = node.next;
}
return counter;
}
getFirst() {
return this.head;
}
getLast() {
if(!this.head){
return null;
}
while(node){
if(!node.next){
return node;
}
node = node.next;
}
}
clear() {
this.head = null;
}
removeFirst() {
if(!this.head){
return;
}
this.head = this.head.next
}
}