-
Notifications
You must be signed in to change notification settings - Fork 8
/
MyStack.java
42 lines (35 loc) · 825 Bytes
/
MyStack.java
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
package stack;
// Source : https://leetcode.com/problems/implement-stack-using-queues/
// Id : 225
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2021/2/11
// Topic : Stack, Queue
// Level : Easy
// Other :
// Tips :
// Links :
// Result : 100% 100%
import java.util.LinkedList;
import java.util.Queue;
public class MyStack {
Queue<Integer> queue;
public MyStack() {
queue = new LinkedList<>();
}
public void push(int x) {
int n = queue.size();
queue.offer(x);
for (int i = 0; i < n; i++) {
queue.offer(queue.poll());
}
}
public int pop() {
return queue.poll();
}
public int top() {
return queue.peek();
}
public boolean empty() {
return queue.isEmpty();
}
}