class Node{
constructor(value)
{
this.data = value;
this.next = null;
}
}
class LinkedList{
constructor(value){
this.head = new Node(value);
this.tail = this.head;
this.length = 1;
}
append(value){
let newNode = new Node(value);
this.tail.next = newNode;
this.tail = newNode;
this.length++;
}
prepend(value)
{
let newNode = new Node(value);
newNode.next = this.head;
this.head = newNode;
this.length++;
}
printList(){
let arr = [];
let currNode = this.head;
while(currNode!=null)
{
arr.push(currNode.data);
currNode = currNode.next;
}
return arr;
}
insertAt(n,value)
{
if(n>=this.length)
return this.append(value);
let count = 0;
let currNode = this.head;
while(count!=n)
{
currNode = currNode.next;
count++;
}
let newNode = new Node(value);
newNode.next = currNode.next;
currNode.next = newNode;
this.length++;
}
remove(n)
{
if(n>=this.length)
return -1;
let count = 0;
let currNode = this.head;
let prev = this.head;
while(count!=n)
{
prev = currNode;
currNode = currNode.next;
count++;
}
prev.next = currNode.next;
currNode=null;
return true;
}
}
let obj = new LinkedList(6);
obj.append(5);
obj.append(7);
obj.prepend(1);
obj.insertAt(100,100);
obj.remove(2);
console.log(obj.printList())