AcWing 41. 包含min函数的栈 (Java)
原题链接
简单
作者:
peilin
,
2020-07-05 16:45:10
,
所有人可见
,
阅读 895
class MinStack {
private Node head;
/** initialize your data structure here. */
public MinStack() {
head = null;
}
public void push(int x) {
if(head == null) {
head = new Node(x, x, null);
} else {
head = new Node(x, Math.min(x, head.min), head);
}
}
public void pop() {
head = head.next;
}
public int top() {
return head.value;
}
public int getMin() {
return head.min;
}
private class Node {
int value;
int min;
Node next;
public Node(int value, int min, Node next) {
this.value = value;
this.min = min;
this.next = next;
}
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/