forked from SamirPaulb/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0622-design-circular-queue.py
45 lines (35 loc) · 1.04 KB
/
0622-design-circular-queue.py
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
class ListNode:
def __init__(self, val):
self.val = val
self.next = None
class MyCircularQueue:
def __init__(self, k: int):
self.head = None
self.tail = None
self.maxSize = k
self.curSize = 0
def enQueue(self, value: int) -> bool:
newNode = ListNode(value)
if self.isEmpty():
self.head = self.tail = newNode
else:
if self.isFull(): return False
self.tail.next = newNode
self.tail = self.tail.next
self.curSize += 1
return True
def deQueue(self) -> bool:
if self.isEmpty(): return False
self.head = self.head.next
self.curSize -= 1
return True
def Front(self) -> int:
return -1 if self.isEmpty() else self.head.val
def Rear(self) -> int:
return -1 if self.isEmpty() else self.tail.val
def isEmpty(self) -> bool:
return self.curSize == 0
def isFull(self) -> bool:
return self.curSize == self.maxSize
# Time: O(1)
# Space: O(k)