-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1670.design-front-middle-back-queue.java
More file actions
63 lines (52 loc) · 1.24 KB
/
1670.design-front-middle-back-queue.java
File metadata and controls
63 lines (52 loc) · 1.24 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
#
# @lc app=leetcode id=1670 lang=java
#
# [1670] Design Front Middle Back Queue
#
import java.util.LinkedList;
# @lc code=start
class FrontMiddleBackQueue {
LinkedList<Integer> list;
public FrontMiddleBackQueue() {
list = new LinkedList<Integer>();
}
public void pushFront(int val) {
list.addFirst(val);
}
public void pushMiddle(int val) {
list.add(list.size() / 2, val);
}
public void pushBack(int val) {
list.addLast(val);
}
public int popFront() {
if (list.isEmpty()) {
return -1;
}
return list.removeFirst();
}
public int popMiddle() {
if (list.isEmpty()) {
return -1;
}
int sz = list.size();
return list.remove((sz - 1) / 2);
}
public int popBack() {
if (list.isEmpty()) {
return -1;
}
return list.removeLast();
}
}
/**
* Your FrontMiddleBackQueue object will be instantiated and called as such:
* FrontMiddleBackQueue obj = new FrontMiddleBackQueue();
* obj.pushFront(val);
* obj.pushMiddle(val);
* obj.pushBack(val);
* int param_4 = obj.popFront();
* int param_5 = obj.popMiddle();
* int param_6 = obj.popBack();
*/
# @lc code=end