add a O(n) time and constant space solution