Coding Interview Questions

How to Reverse a Linked List – Interview Coding Question

How would you reverse a singly linked list?

Why Interviewers Ask This

Reversing a linked list tests your understanding of pointer manipulation, data structure fundamentals, and the ability to think through state changes step by step. It is one of the most frequently asked coding interview questions at all levels.

How to Answer

  1. 1

    Clarify whether the interviewer wants an iterative or recursive approach, or both.

  2. 2

    For iterative: use three pointers (prev, current, next) and walk through the list, reversing each link.

  3. 3

    For recursive: reverse the rest of the list first, then fix the head pointer.

  4. 4

    Discuss time complexity O(n) and space complexity O(1) for iterative, O(n) for recursive due to call stack.

Sample Answer

To reverse a singly linked list iteratively, I initialize three pointers: prev as null, current as head, and next as null. I iterate through the list — at each step I save current.next into next, point current.next to prev, advance prev to current, and advance current to next. When current becomes null, prev is the new head. This runs in O(n) time and O(1) space. For the recursive approach, I recurse to the end of the list, then on the way back I set each node's next pointer to the previous node. The base case is when the node is null or has no next node. The recursive version also runs in O(n) time but uses O(n) space on the call stack. I prefer the iterative approach for production code since it avoids stack overflow on very long lists.

Pro Tips

  • Draw the pointer changes on a whiteboard — visual thinking prevents off-by-one errors.

  • Always handle edge cases: empty list, single-node list.

  • Mention that in-place reversal is preferred to creating a new list to save memory.

Frequently Asked Questions

Can you reverse a linked list in place?

Yes. The iterative three-pointer approach reverses the list in place with O(1) extra space.

Which approach is better: iterative or recursive?

Iterative is generally preferred because it uses O(1) space and avoids potential stack overflow on long lists.

What if the list is doubly linked?

For a doubly linked list, swap the prev and next pointers of every node, then update the head to the old tail.

Worried About Going Blank in Your Interview?

HireMe AI listens to your interview in real-time, detects questions, and generates instant answer suggestions — so you never freeze up again.

Try HireMe AI Free