LinkedList Solution

Run Settings
LanguageJavaScript
Language Version
Run Command
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);
Editor Settings
Theme
Key bindings
Full width
Lines