150. Evaluate Reverse Polish Notation | LeetCode | Top Interview 150 | Coding Questions
Published: (February 11, 2026 at 02:19 PM EST)
1 min read
Source: Dev.to
Source: Dev.to
Problem
Evaluate Reverse Polish Notation – LeetCode
Solution discussion
Solution
class Solution {
public int evalRPN(String[] tokens) {
Stack stack = new Stack<>();
for (String token : tokens) {
if (token.equals("+") || token.equals("-") ||
token.equals("*") || token.equals("/")) {
int b = stack.pop();
int a = stack.pop();
int res = operate(a, b, token);
stack.push(res);
} else {
stack.push(Integer.parseInt(token));
}
}
return stack.pop();
}
private int operate(int a, int b, String token) {
switch (token) {
case "+" : return a + b;
case "-" : return a - b;
case "*" : return a * b;
case "/" : return a / b;
default : return 0;
}
}
}