in O(n)runtime, O(n)Space