a better way to solve it. No extra space needed.