We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I think, you dont need an additional else in the code if the two is not empty.Here is my code :
publicstaticvoidmain(String[]args){/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */Scannerin=newScanner(System.in);intnoElements=in.nextInt();Stack<Integer>one=newStack<Integer>();Stack<Integer>two=newStack<Integer>();intcommand;for(inti=0;i<noElements;i++){command=in.nextInt();if(command==1){one.push(in.nextInt());}elseif(command==2){if(two.isEmpty()){while(!one.isEmpty()){two.push(one.pop());}}two.pop();}elseif(command==3){if(two.isEmpty()){while(!one.isEmpty()){two.push(one.pop());}}System.out.println(two.peek());}}}
Cookie support is required to access HackerRank
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 →
I think, you dont need an additional else in the code if the two is not empty.Here is my code :