DSA interview question
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.
Node* reverseRecursive(Node* head); // returns the new head
Node* reverseIterative(Node* head); // three-pointer version, for comparison
| Input list | Output list | Explanation |
|---|---|---|
1 -> 2 -> 3 -> 4 -> 5 | 5 -> 4 -> 3 -> 2 -> 1 | |
1 -> 2 | 2 -> 1 | |
7 | 7 | Base case: one node |
(empty) | (empty) | Base case: no nodes |
(Confirm: links are reversed in place; both versions must produce the same result.)
head->next->next = head?"head->next = nullptr?"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 interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in