class Node {
constructor(val, min, next = null) {
this.val = val
this.min = min
this.next = next
}
}
class MinStack {
constructor() {
this.head = null
this.min = null
}
push(val) {
if(!this.head) {
this.head = new Node(val, val)
this.min = val
} else {
const temp = this.head
this.min = Math.min(val, this.min)
this.head = new Node(val, this.min)
this.head.next = temp
}
}
pop() {
if(!this.head) return
const popVal = this.head.val
this.head = this.head.next
this.min = this.head && this.head.min
return popVal
}
getMin() {
return this.min
}
top() {
return this.head.val
}
}