constant space solution to connect