Implement Stack using Queues in Java

Day 83/100 🚀 | #100DaysOfDSA Solved LeetCode 225 – Implement Stack using Queues today. The problem was to implement stack operations (LIFO) using only queue operations (FIFO). Approach: Used a single queue with rotation technique. • For push(x): Added the element to the queue Rotated the queue by moving previous elements to the back This ensures the newest element comes to the front (stack top) • For pop(): Simply removed from the front of the queue • For top(): Returned the front element • For empty(): Checked if queue is empty This way, the queue always maintains stack order. Time Complexity: • push → O(n) • pop → O(1) • top → O(1) Space Complexity: O(n) Key takeaway: By reordering elements during insertion, we can simulate stack behavior using a single queue efficiently. #100DaysOfDSA #LeetCode #DSA #Java #Queue #Stack #ProblemSolving #Consistency

  • text

To view or add a comment, sign in

Explore content categories