add a O(n) time and O(1) space solution