You have two pointers, one at the very beginning (index i) and one at the very end (index j) of the line. The item at the beginning of the list swaps places with the item at the end of the list. The pointer at the beginning moves one place to the right, and the pointer at the end moves one place to the left. Keep swapping and moving the pointers until they meet or cross each other in the middle of the line.
ls = [1, 2, 3, 4, 5, 6, 7, 8, 9] i = 0 j = len(ls) - 1 while i < j: t = ls[i] ls[i] = ls[j] ls[j] = t i += 1 j -= 1 print(ls)
const list = [1, 2, 3, 4, 5, 6, 7, 8, 9]; for (let i = 0, j = list.length - 1; i < j; i++, j--) { const t = list[j]; list[j] = list[i]; list[i] = t; } console.log(list);