avatar

superorange0707

  • Homepage
  • Tag
  • Category
Home Leetcode-232-Implement Queue using Stacks
文章

Leetcode-232-Implement Queue using Stacks

Posted 2022-07-16
6~7 min read

Implement Queue using Stacks

leetcode: https://leetcode.com/problems/implement-queue-using-stacks/

Description:

Given the head of a linked list, return the node where the cycle begins. If there is no cycle, return null.

There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail’s next pointer is connected to (0-indexed). It is -1 if there is no cycle. Note that pos is not passed as a parameter.

Do not modify the linked list.

Idea:

**solution **

Step1: create two stack, one for in, one for out

Step2: push all elements to stack1, and then when pop element, add all stack1’s elements to stack2. It will change the Fisrt in Last out to First In First out, because stack pop the element from the top of the stack.

Step3: the same operations of peek method

Code:

class MyQueue {
    //define two stack, one for in, the other for out
    Stack<Integer> StackIn;
    Stack<Integer> StackOut;
    public MyQueue() {
        //create two stack instance;
        StackIn = new Stack<>();
        StackOut = new Stack<>();
    }
    
    public void push(int x) {
        //add elements into the Stack(IN)
        StackIn.push(x);
        
    }
    
    public int pop() {
        //extract elements from StackIN and add them to the StackOut
        if(StackOut.isEmpty()){
            //extract all StackIn elements and put them to the outStack
            while(!StackIn.isEmpty()){
                StackOut.push(StackIn.pop());
            }
        }
        return StackOut.pop();
        
    }
    
    public int peek() {
        //get StackOut elements from StackIn
        if(StackOut.isEmpty()){
            while(!StackIn.isEmpty()){
                StackOut.push(StackIn.pop());
            }
        }
        return StackOut.peek();
    }
    //if two stack are both empty
    public boolean empty() {
        return StackIn.isEmpty() && StackOut.isEmpty() ;
    }
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */
Leetcode
Leetcode Stack and Queue
Share

Further Reading

Apr 23, 2025

283 - Move Zero

[283 - Move Zero] 🔗 LeetCode Link Problem Description Given an integer array nums, move all 0's to the end of it while maintaining the relative order

Apr 23, 2025

27 - Remove Element

[27 - Remove Element] 🔗 LeetCode Link Problem Description Given an integer array nums and an integer val, remove all occurrences of val in nums in-pl

Apr 23, 2025

26 - Remove Duplicates from Sorted Array

[26 - Remove Duplicates from Sorted Array] 🔗 LeetCode Link Problem Description Given an integer array nums sorted in non-decreasing order, remove the

OLDER

Leetcode-142-Linked List Cycle II

NEWER

Leetcode-225-Implement Stack using Queues

Recently Updated

  • Migrating Jenkins SCM Using GitLab from Bitbucket: SCM URL Bulk Replacement
  • 283 - Move Zero
  • 27 - Remove Element
  • 26 - Remove Duplicates from Sorted Array
  • Migrating from Bitbucket to GitLab? Here’s how to keep your teams moving without missing a beat!

Trending Tags

Course two pointer Binary Tree Hash SQL Leetcode Error Recording Gitlab Bitbucket Devops

Contents