Skip to content

visual walkthrough

Evaluate Reverse Polish Notation

MediumExpression EvaluationReported at: GoogleAmazonMeta+3

Stack-based expression evaluation, the basis of calculators.

Solve on LeetCode

The idea

In Reverse Polish Notation an operator always applies to the two most recent results. A stack keeps exactly those: numbers are pushed, and an operator pops two, computes, and pushes the answer.

Pop order matters for − and ÷: the first pop is the right operand.

Complexity

approachtimespace
Reduce the listO(n²)O(n)
StackO(n)O(n)

More walkthroughs