标签:
Implement the following operations of a stack using queues.
push
to back
, peek/pop from front
, size
,
and is empty
operations are valid.class MyStack { LinkedList<Integer> queue = new LinkedList<Integer>(); // Push element x onto stack. public void push(int x) { queue.add(x); for (int i = 0; i < queue.size()-1; i++) { queue.add(queue.poll()); } } // Removes the element on top of the stack. public void pop() { queue.poll(); } // Get the top element. public int top() { return queue.peek(); } // Return whether the stack is empty. public boolean empty() { return queue.isEmpty(); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
LeetCode-Implement Stack using Queues
标签:
原文地址:http://blog.csdn.net/my_jobs/article/details/47620139