2
// O(1) for init and methods other than pushMiddle(), popMiddle()3
// O(N) for pushMiddle(), popMiddle()4
// S.C: O(N) for storage6
// Doubly linked list node7
function ListNode(val, prev, next) {9
this.prev = prev || null;10
this.next = next || null;13
var FrontMiddleBackQueue = function () {23
FrontMiddleBackQueue.prototype.pushFront = function (val) {25
this.head = new ListNode(val);26
this.tail = this.head;31
let newNode = new ListNode(val);32
newNode.next = this.head;33
this.head.prev = newNode;34
this.head = newNode; // the new node becomes the new head42
FrontMiddleBackQueue.prototype.pushMiddle = function (val) {48
for (let i = 1; i < Math.floor(this.len / 2); i++) {49
// we go to previous node of median52
let newNode = new ListNode(val);57
if (next) next.prev = newNode;65
FrontMiddleBackQueue.prototype.pushBack = function (val) {71
let newNode = new ListNode(val);72
this.tail.next = newNode;73
newNode.prev = this.tail;74
this.tail = newNode; // end node becomes the new tail81
FrontMiddleBackQueue.prototype.popFront = function () {85
let front = this.head;86
let next = front.next;87
if (next) next.prev = null;96
FrontMiddleBackQueue.prototype.popMiddle = function () {98
return this.popFront();101
for (let i = 1; i < Math.ceil(this.len / 2); i++) {102
// we go to median node110
if (prev) prev.next = next;111
if (next) next.prev = prev;119
FrontMiddleBackQueue.prototype.popBack = function () {121
return this.popFront();133
* Your FrontMiddleBackQueue object will be instantiated and called as such:134
* var obj = new FrontMiddleBackQueue()136
* obj.pushMiddle(val)138
* var param_4 = obj.popFront()139
* var param_5 = obj.popMiddle()140
* var param_6 = obj.popBack()