Third iteration using only stacks