LinkedList Solution
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
class Ourlist{
constructor(value){
this.head = {
value: value,
next: null,
}
this.tail = this.head;
this.length = 1;
}
append(value){
//code here
const newNode ={
value : value,
next : null
};
this.tail.next = newNode;
this.tail = newNode;
this.length ++;
return this;
}
PrintTree(temp_head){
//
this.tail = this.head;
while(this.tail.next !== null){
console.log(this.tail.next);
this.tail = this.tail.next;
}
}
Reverse(){
if(!this.head.next){
return this.head;
}
let first = this.head;
this.tail = this.head
let second = first.next;
while(second){
const temp = second.next;
second.next = first;
first = second;
second = temp;
}
this.head.next = null;
this.head = first;
return this.PrintTree(this.head);
}
}
const LinkedList = new Ourlist(10);
let temp;
LinkedList.append(10);
LinkedList.append(16);
LinkedList.append(5);
LinkedList.append(22);
LinkedList.append(32);
LinkedList.PrintTree(temp);
LinkedList.Reverse(LinkedList);
//console.log(LinkedList);
JavaScript
INFO