swap-with-prev function