add O(1) space solution