Data Structures19 sections · 729 units
Open in Course

Deque vs Two Stacks

Alternative approach

You can simulate a deque using two stacks. One stack for the front, one for the back. When one is empty and you need to access it, move elements from the other.

You get amortized O(1)O(1) per operation. It's rarely needed in contests, but useful if the language doesn't have a built-in deque.

Stacks and queues are fundamentally related. Deques unify both.