// O(n)
enqueue(x):
if stack.isEmpty():
stack.push(x)
return
temp = stack.pop()
enqueue(x)
stack.push(temp)
// O(1)
x dequeue():
return stack.pop()
// O(1)
enqueue(x):
stack.push(x)
// O(n)
x dequeue():
temp = stack.pop()
if stack.isEmpty():
x = temp
else:
x = dequeue()
stack.push(temp)
return x
[Hat tip to SO]
No comments:
Post a Comment