您的位置:首页 > 产品设计 > UI/UE

[LeetCode]Implement Stack using Queues

2015-12-05 13:34 337 查看
class MyStack {
// Push element x onto stack.
Queue<Integer> queue = new LinkedList<Integer>();
public void push(int x) {
Queue<Integer> q = new LinkedList<Integer>();
q.offer(x);
while (!queue.isEmpty()) {
q.offer(queue.poll());
}
queue = q;
}

// 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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: