1. class Node {
    2. constructor(val, min, next = null) {
    3. this.val = val
    4. this.min = min
    5. this.next = next
    6. }
    7. }
    8. class MinStack {
    9. constructor() {
    10. this.head = null
    11. this.min = null
    12. }
    13. push(val) {
    14. if(!this.head) {
    15. this.head = new Node(val, val)
    16. this.min = val
    17. } else {
    18. const temp = this.head
    19. this.min = Math.min(val, this.min)
    20. this.head = new Node(val, this.min)
    21. this.head.next = temp
    22. }
    23. }
    24. pop() {
    25. if(!this.head) return
    26. const popVal = this.head.val
    27. this.head = this.head.next
    28. this.min = this.head && this.head.min
    29. return popVal
    30. }
    31. getMin() {
    32. return this.min
    33. }
    34. top() {
    35. return this.head.val
    36. }
    37. }