var MinStack = function () {
this.Node = function Node(value, min) {
this.value = value;
this.min = min;
this.next = null;
};
this.head = null;
};
* @param {number} x
* @return {void}
*/
MinStack.prototype.push = function (x) {
if (!this.head) {
this.head = new this.Node(x, x);
} else {
const newHead = new this.Node(x, Math.min(x, this.head.min));
newHead.next = this.head;
this.head = newHead;
}
};
* @return {void}
*/
MinStack.prototype.pop = function () {
if (this.head) {
this.head = this.head.next;
}
};
* @return {number}
*/
MinStack.prototype.top = function () {
if (this.head) {
return this.head.value;
}
};
* @return {number}
*/
MinStack.prototype.getMin = function () {
if (this.head) {
return this.head.min;
}
};
评论