class Solution { public boolean validateStackSequences(int[] pushed, int[] popped) { Deque<Integer> q = new LinkedList<>(); int N = pushed.length; int j = 0; for(int x:pushed) { q.push(x); while(q.isEmpty()==false && q.peek()==popped[j]) { q.pop(); ++j; } } return q.isEmpty(); } }