Add iterative solution with constant space.