linear time, constant space,AC