Overview
Reverse a list by swapping elements from both ends moving inward, or use Python's built-in reverse.
Analogy
Flipping a deck of cards: swap top and bottom, then second and second-from-bottom, meeting in the middle.
Step-by-step
- Built-in: lst[::-1] returns a new reversed list.
- In-place: lst.reverse() modifies the list.
- Manual two-pointer: i=0, j=len-1; swap while i<j; i++, j--.
Visual
[1,2,3,4,5] → swap(1,5) → swap(2,4) → [5,4,3,2,1]
Common mistakes
- Using lst[::-1] when you need in-place (creates unnecessary copy).
- Off-by-one stopping condition: stop when i >= j.
Practice questions
- Reverse a string using two-pointer approach.
- Check if a list is a palindrome by comparing to its reverse.
Space
O(1) in-place, O(n) for slice