MCPcopy Create free account
hub / github.com/DestinationFAANG/Destination-FAANG-Java-Solution / MinStack

Class MinStack

155 Min Stack/155 Min Stack.java:10–49  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8
9
10class MinStack {
11 private Node head;
12
13 /** initialize your data structure here. */
14 public MinStack() {
15
16 }
17
18 public void push(int val) {
19 if (head == null) {
20 head = new Node(val, val, null);
21 } else {
22 head = new Node(val, Math.min(val, head.min), head);
23 }
24 }
25
26 public void pop() {
27 head = head.next;
28 }
29
30 public int top() {
31 return head.val;
32 }
33
34 public int getMin() {
35 return head.min;
36 }
37
38 private class Node {
39 int val;
40 int min;
41 Node next;
42
43 Node(int val, int min, Node next) {
44 this.val = val;
45 this.min = min;
46 this.next = next;
47 }
48 }
49}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected