PPractick

DSA interview question

Reverse a Linked List (Iterative and Recursive)

Medium · Data structures · Topics: linkedlist, recursion

The problem

After writing the iterative reversal, the interviewer asks the natural follow-up: "Now do it recursively." The recursive version is short but subtle. Trust the recursive call to reverse everything after the head, then attach the head at the end of that reversed part. Because the head's old successor is now the last node of the reversed sublist, head->next->next = head does exactly that. Understanding why this works is a good test of recursive thinking on linked structures.

Example: 1 -> 2 -> 3 -> 4 -> 5 → 5 -> 4 -> 3 -> 2 -> 1.

Function signature

Node* reverseRecursive(Node* head);   // returns the new head
Node* reverseIterative(Node* head);   // three-pointer version, for comparison

Test cases

Input listOutput listExplanation
1 -> 2 -> 3 -> 4 -> 55 -> 4 -> 3 -> 2 -> 1
1 -> 22 -> 1
77Base case: one node
(empty)(empty)Base case: no nodes

(Confirm: links are reversed in place; both versions must produce the same result.)

How interviewers phrase it

Clarifying questions to ask

  1. Is a recursive solution acceptable for long lists? (Stack depth equals the list length.)
  2. In place?
  3. Return the new head?

Follow-ups to expect

  1. "What does the recursive call return?"
  2. "Why head->next->next = head?"
  3. "Why set head->next = nullptr?"
  4. "Which version would you use in production?"
  5. "Complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in