Java Why can use peek() in top?

  • 0

    In Stack ,top() means getting the element at the top of the stack.And peek() in Queue means getting the head of the queue. Why do some one use peek() in top() ,and it can AC?

  • 1

    I think that the reason is the solution to solve the problem using 2 queue. When a new item is pushed, the queues reverse the order. So when top() run, just using queue.peek() could complete the work.

Log in to reply

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