Implement Stack using Queues in Java

Day 26 of Daily DSA 🚀 Solved LeetCode 225: Implement Stack using Queues ✅ Problem: Implement a LIFO stack using only queue operations. Approach: Used two queues: main queue → stores elements in stack order helper queue → assists during push operation Key idea: Move all elements from main to helper Insert the new element into main Move all elements back from helper to main This ensures the latest pushed element stays at the front, allowing pop() and top() in constant time. ⏱ Complexity: • push() → O(n) • pop() → O(1) • top() → O(1) • empty() → O(1) 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 42.54 MB (Beats 89.59%) A great problem to understand how one data structure can simulate another. #DSA #LeetCode #Java #DataStructures #CodingJourney #ProblemSolving #Consistency

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories