alternative solution to merge without introducing extra linear space