Javascript Code
Javascript Code
constructor (val) {
this.val = val;
this.next = null;
}
}
insertFront(val) {
const newNode = new DoublyListNode(val);
newNode.prev = this.head;
newNode.next = this.head.next;
this.head.next.prev = newNode;
this.head.next = newNode;
}
insertEnd(val) {
const newNode = new DoublyListNode(val);
newNode.next = this.tail;
newNode.prev = this.tail.prev;
this.tail.prev.next = newNode;
this.tail.prev = newNode;
}
print() {
let curr = this.head.next;
let s = "";
while (curr != this.tail) {
s+= curr.val + "->";
curr = curr.next;
}
console.log(s);
}
}