-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMinStack.java
More file actions
50 lines (38 loc) · 744 Bytes
/
Copy pathMinStack.java
File metadata and controls
50 lines (38 loc) · 744 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* https://leetcode.com/problems/min-stack/
*/
class Node{
int x;
int minimum;
Node next;
public Node(int x, int minimum) {
this.x = x;
this.minimum = minimum;
}
}
class MinStack {
Node head;
public MinStack() {
head = null;
}
public void push(int x) {
int minimum;
if (head == null) {
minimum = x;
}else {
minimum = Math.min(head.minimum, x);
}
Node curr = new Node(x, minimum);
curr.next = head;
head = curr;
}
public void pop() {
head = head.next;
}
public int top() {
return head.x;
}
public int getMin() {
return head.minimum;
}
}