DSA interview question
A messaging app stores a chat's messages as a singly linked list from oldest to newest, and the "newest first" view needs the list reversed in place, without allocating a second list. In a singly linked list each node only knows its successor, so reversing means redirecting every next pointer to point backwards. Doing that without losing the rest of the list takes three pointers: the part already reversed (prev), the node being processed (cur), and the part still to process (next). It's probably the most-asked linked-list question of all.
Example: 1 -> 2 -> 3 -> 4 -> 5 → 5 -> 4 -> 3 -> 2 -> 1.
Node* reverseList(Node* head); // returns the new head
| Input list | Output list | Explanation |
|---|---|---|
1 -> 2 -> 3 -> 4 -> 5 | 5 -> 4 -> 3 -> 2 -> 1 | |
1 -> 2 | 2 -> 1 | |
7 | 7 | |
(empty) | (empty) |
(Confirm: reverse the links in place (O(1) extra space); return the new head.)
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