Reversing Linked Lists in Coding Interviews: Iterative, Recursive, and the Sublist Variants
3 views
Reversing a linked list is one of the first problems many candidates practice, and it is still asked at every level because it quickly reveals whether you can manipulate pointers without losing track of nodes. More importantly, it is the building block for harder questions: reversing a sublist, reversing in groups of k, checking palindromes, and reordering lists. Mastering the basic move makes all of these manageable.
The iterative reversal
Keep three pointers: prev, curr, and next. At each step, save curr.next, point curr.next back to prev, then advance both prev and curr.
def reverse(head):
prev = None
curr = head
while curr:
nxt = curr.next
curr.next = prev
prev = curr
curr = nxt
return prev
Time O(n), space O(1). The most common bug is overwriting curr.next before saving it, which loses the rest of the list. Saying "save next first" out loud helps you and the interviewer.
The recursive reversal
Reverse the rest of the list, then attach the current node at the end.
def reverse_rec(head):
if not head or not head.next:
return head
new_head = reverse_rec(head.next)
head.next.next = head
head.next = None
return new_head
It is elegant but uses O(n) stack space, which matters for very long lists. Mention that trade-off.
Variant 1: Reverse a sublist between positions left and right
Use a dummy node before the head so the edge case where left is 1 needs no special handling. Walk to the node just before position left, then repeatedly move the next node to the front of the sublist.
def reverse_between(head, left, right):
dummy = ListNode(0, head)
before = dummy
for _ in range(left - 1):
before = before.next
curr = before.next
for _ in range(right - left):
moved = curr.next
curr.next = moved.next
moved.next = before.next
before.next = moved
return dummy.next
Variant 2: Reverse nodes in k-groups
Check that k nodes remain, reverse those k nodes with the standard loop, connect the previous group's tail to the new head, and continue. Leave the final group unchanged if it has fewer than k nodes. This is a common hard-level question, and the dummy node plus a clear "group tail" pointer keeps it manageable.
Variant 3: Palindrome linked list
Find the middle with fast and slow pointers, reverse the second half, compare the two halves, and optionally restore the list. This combines two patterns in O(n) time and O(1) space.
Variant 4: Reorder list
Rearrange L0, L1, ..., Ln into L0, Ln, L1, Ln-1, and so on. Find the middle, reverse the second half, then merge the two halves alternately.
Tips for interviews
- Draw three or four boxes and arrows before coding. Interviewers like seeing you reason visually.
- Use a dummy node whenever the head might change.
- Test with an empty list, a single node, and two nodes.
- Narrate pointer changes step by step instead of coding silently.
Pointer problems punish small slips, and nerves make slips more likely. Linkjob AI (https://linkjob.ai) provides real-time help during job interviews and technical interviews, helping you keep your explanation and steps organized as you code. It is most effective when paired with real practice.
Takeaway: learn the three-pointer reversal until it is automatic, and many harder linked list questions become combinations of moves you already know.