DoublyLinkedList = {
class DoublyLinkedList {
static of(...nodes) {
const list = new DoublyLinkedList;
for (const node of nodes) list.insert(node);
return list;
}
constructor() {
this.head = null;
this.tail = null;
}
insert(node, where = null) {
node.next = where;
node.prev = where ? where.prev : this.tail;
if (where === this.head) this.head = node;
if (where === null) this.tail = node;
if (node.prev) node.prev.next = node;
if (node.next) node.next.prev = node;
return this;
}
remove(node) {
if (node.next) node.next.prev = node.prev;
else this.tail = node.prev;
if (node.prev) node.prev.next = node.next;
else this.head = node.next;
node.next = null;
node.prev = null;
return this;
}
}
DoublyLinkedList.Node = class Node {
constructor(value) {
this.value = value;
this.next = null;
this.prev = null;
}
};
return DoublyLinkedList;
}