improved node swapping at index 0 and -1.