You are viewing a single comment's thread. Return to all comments →
Thank you for your solution. I had the same timeout issue. Below I rewrote a shorter solution.
private static void processCommand( Stack<Integer> one, Stack<Integer> two, int command, int arg) { if (command == 1) { one.push(arg); } else { if (two.isEmpty()) { while(!one.isEmpty()) { two.push(one.pop()); } } if (command == 2){ two.pop(); } else if (command == 3) { System.out.println(two.peek()); } } }
Seems like cookies are disabled on this browser, please enable them to open this website
Queue using Two Stacks
You are viewing a single comment's thread. Return to all comments →
Thank you for your solution. I had the same timeout issue. Below I rewrote a shorter solution.