class Node {
constructor(val){
this.next=undefined;
this.val=val
}
}
class LinkList{
constructor(){
this.head=undefined;
this.tail=undefined;
this.length=0;
}
reverse(){
let curr=this.head;
let prevNode=undefined;
this.tail=curr;
while(curr){
const tempNode=curr.next;
curr.next=prevNode;
prevNode=curr;
curr=tempNode
}
this.head=prevNode;
}
delete(val){
let curr=this.head;
while(curr.next){
if(curr.next.val===val){
let deleteNode=curr.next
curr.next=curr.next.next;
deleteNode.next=null
deleteNode=null
}else{
curr=curr.next;
}
}
return this
}
pop(){
let curr=this.head;
while(curr.next.next){
curr=curr.next;
}
let deleteNode=curr.next;
curr.next=undefined;
this.tail=curr
deleteNode.next=null;
deleteNode=null;
return this
}
shift(){
if(!this.head){
console.log('no node available');
}else{
let node = this.head;
this.head=this.head.next;
console.log(`deleteting node with value:${node.val}`);
node.next=null;
node=null
}
return this;
}
unshift(val){
const newNode = new Node(val);
if(!this.head){
this.head=newNode;
this.tail=this.head;
}else{
newNode.next=this.head;
this.head=newNode;
}
return this;
}
push(val){
const newNode = new Node(val)
if(!this.head){
this.head=newNode;
this.tail=this.head;
}else{
this.tail.next=newNode;
this.tail=newNode;
}
return this;
}
traverse(){
let result=''
let curr=this.head;
while(curr){
if(!curr.next){
result+=`${curr.val}==> null`;
}else{
result+=`${curr.val}==> `;
}
curr=curr.next
}
console.log(result);
return this;
}
}
const linklist = new LinkList;
linklist.unshift(1);
linklist.unshift(0);
linklist.push(2);
linklist.push(3);
linklist.push(4);
linklist.push(5);
linklist.traverse()
linklist.reverse()
linklist.pop()
linklist.pop()
linklist.traverse()