C# solution without using two stacks and pretty straightforward


  • 0
    C
    public class MyQueue {
    
        int val;
        List<int> queue;
        /** Initialize your data structure here. */
        public MyQueue() {
            queue = new List<int>();
        }
        
        /** Push element x to the back of queue. */
        public void Push(int x) {
            queue.Add(x);
        }
        
        /** Removes the element from in front of queue and returns that element. */
        public int Pop() {
            int x = queue[0];
            queue.RemoveAt(0);
            return x;
        }
        
        /** Get the front element. */
        public int Peek() {
            //int[] arr = queue.ToArray();
            return queue[0];
        }
        
        /** Returns whether the queue is empty. */
        public bool Empty() {
            return queue.Count==0;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.