1. Three pointers
Instead of building a new list, we turn each arrow around as we walk along. That needs three references: previous (the part already reversed), current (the node being changed) and following (the rest of the list).
- Save
current.nextinfollowingfirst, or the rest of the list is lost. - Point
current.nextback atprevious. - Move
previousandcurrentforward one node and repeat.
2. Tracing 1 → 2 → 3
After the first pass, 1 points to nothing. After the second, 2 points to 1. After the third, 3 points to 2, current becomes None and the loop stops, returning 3 as the new head.
3. Edge cases
An empty list (head is None) skips the loop and returns None. A single node is returned unchanged. Both are worth including in your tests.