Linked List — Pointer Surgery
Redraw next pointers carefully; draw on paper first.
When to use this
- Reverse
- Merge sorted
- Cycle (Floyd)
C++ template
ListNode* prev = nullptr;
ListNode* cur = head;
while (cur) {
ListNode* nxt = cur->next;
cur->next = prev;
prev = cur;
cur = nxt;
}
return prev;Dry run
1→2→3 reverse → 3→2→1.
Pitfalls
- Losing next
- No dummy on merge
- Cycle entry mistakes
Say this in the interview
prev/cur/next so I never lose the list.
Problems in this topic (3)
Each opens teach-first (animated when available), then the code panel.