class linkedList{
constructor(value){
this.head={
value:value,
next:null
};
this.tail=this.head;
this.length=1;
}
append(value){
const newNode={
value:value,
next:null
};
this.tail.next=newNode;
this.tail=newNode;
this.length++;
}
printList() {
const array=[];
let currNode=this.head;
while(currNode != null){
array.push(currNode.value);
currNode=currNode.next;
}
return array;
}
insert(index,value){
if(index >= this.length){
return this.append(value);
}
const newNode{
value:value,
next:null
}
const leader= this.traverseToIndex(index-1);
const holdingpointer=currNode.next;
leader.next=newNode;
newNode.next=holdingpointer;
this.length++;
return this.printlist()
}
remove(index){
const leader=this.traverseToIndex(index-1);
const unwantedNode = leader.next;
leader.next=unwantedNode.next;
this.length--;
return this.printList();
}
traverseToIndex(index){
let counter=0;
let currNode=this.head;
while(counter !== index){
currNode=currNode.next;
counter++;
}
return currNode;
}
prepend(value){
const newNode={
value:value,
next:null
};
this.head.next=newNode;
this.head=newNode;
this.length++;
}
}
const newLinkedList = new linkedList(10);
newLinkedList.append(5);
newLinkedList.append(16);
newLinkedList.prepend(1);
newLinkedList.insert(2,99);
newLinkedList.printList();
//console.log(newLinkedList);