206. Reverse Linked List
- Паттерн:
- in-place pointer reversal
- Время:
- O(n)
- Память:
- O(1)
- Написано:
Заметки написаны на английском.
Задача
1 -> 2 -> 3 -> 4 -> null (before)
null <- 1 <- 2 <- 3 <- 4 (after — head is now 4)Подход
prev, curr) and one sticky note (nextTemp) so I never lose the rest of the list mid-flip.
Решение
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode nextTemp = curr.next; // 1. save next BEFORE overwriting
curr.next = prev; // 2. flip the arrow
prev = curr; // 3. prev catches up to curr
curr = nextTemp; // 4. curr moves to the saved node
}
return prev; // when curr hits null, prev is the new head
}Recursive version (kept for reference, not submission)
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) return head;
ListNode newHead = reverseList(head.next);
head.next.next = head; // the node after head now points back to head
head.next = null; // head becomes the tail, so it points to nothing
return newHead;
}head.next.next = head only makes sense once you accept that head.next is still the last node of the already-reversed sublist at that point in the call — it hasn't been touched yet by this frame.
Ошибки
Выводы
The one line order that actually matters
nextTemp = curr.next must happen before curr.next = prev.
x = x++ on the OCA exam: statement order changes the outcome when you're reading and writing the same variable in one step.
Why iterative and not recursion by default
Iterative: 3 reference variables total, no matter how long the list is. Safe. Recursive: elegant, but every node = one stack frame. Long list on LeetCode → StackOverflowError. That's a realErrorsubclass from OCA material, not something you're meant to catch and recover from — it means "this approach doesn't scale," full stop.
Java 8
var, no lambdas — just while, references, and object creation. Fully valid on Java 8 and honestly on Java 1.0 too.
Время и память
Проверить себя
Can I write the iterative version from memory without looking? Can I explain out loud why nextTemphas to come first, without the kids-holding-hands analogy as a crutch?What breaks if I return currinstead ofprevat the end? (Answer:currisnullby the time the loop exits — that's the whole reason we neededprevin the first place.)