Fourth iteration using two stacks