How do you reverse a linked list iteratively versus recursively?

DSA
Medium
51.7K views

This question requires implementing the reversal of a singly linked list using both iterative and recursive methods. It tests pointer manipulation and recursion stack understanding.

Why Interviewers Ask This

Linked list reversal is a staple interview question because it directly tests core data structure manipulation skills. Interviewers want to see if you understand pointer references and how to break and rebuild links. The recursive version specifically checks your comfort with the call stack and base cases. It also reveals your ability to write clean, readable code for complex pointer operations.

How to Answer This Question

First, explain the iterative approach using three pointers: previous, current, and next. Detail how to update pointers to reverse the direction of links one by one. Then, transition to the recursive method, explaining the base case of an empty or single-node list. Describe how the recursion unwinds to link nodes back in reverse order. Compare the space complexity of both approaches.

Key Points to Cover

  • Pointer manipulation technique
  • Base case handling in recursion
  • Space complexity comparison
  • Handling null pointers

Sample Answer

For the iterative solution, I initialize three pointers to null, head, and head.next. I loop through the list, updating the current node's next pointer to point to the previous node, then shifting all three pointers forward. For the recursive approach, the base case returns the last node, and subsequent calls link the current node to the reversed sublist. The iterative method uses constant space, while the recursive method uses O(n) stack space.

Common Mistakes to Avoid

  • Creating cycles in the list
  • Losing reference to the head
  • Stack overflow on large lists

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 49 DSA questions