time o(n) space O(1)