function StackMin() {
this.top = null;
this.size = 0;
this.minStack = [];
}
function Node(data) {
this.data = data;
this.next = null;
}
StackMin.prototype.push = function(data) {
if (!this.minStack.length) {
this.minStack.push(data);
} else if (data < this.minStack[this.minStack.length-1]) {
this.minStack.push(data);
}
const node = new Node(data);
node.next = this.top;
this.top = node;
this.size++;
};
StackMin.prototype.pop = function() {
if (this.top === null) return;
const data = this.top.data;
if (this.minStack[this.minStack.length-1] === this.top.data) this.minStack.pop();
this.top = this.top.next;
this.size = Math.max(0, this.size - 1);
return data;
};
StackMin.prototype.peek = function() {
if (!this.top) return;
return this.top.data;
};
StackMin.prototype.isEmpty = function() {
return this.size === 0 && !this.top;
};
StackMin.prototype.min = function() {
return this.minStack[this.minStack.length-1];
};