Reverse a Linked List

Data Structures
Medium
Google
45.9K views

Given the head of a singly linked list, reverse the list, and return the reversed list.

Why Interviewers Ask This

Google uses this classic problem to assess a candidate's mastery of pointer manipulation and memory management fundamentals. It evaluates whether you can traverse a data structure without auxiliary space, handle edge cases like null pointers or single-node lists, and think logically about state transitions in an iterative or recursive manner.

How to Answer This Question

1. Clarify the constraints immediately: ask if the list is singly linked, what happens with an empty input, and whether recursion is allowed given Google's focus on stack depth. 2. Propose the optimal iterative approach first to demonstrate efficiency awareness, explaining that it uses O(1) space compared to O(n) for recursion. 3. Walk through the logic using three pointers: 'previous', 'current', and 'next'. Explicitly describe how 'current.next' must be updated to point to 'previous' before moving forward. 4. Trace a small example manually (e.g., 1->2->3) on a whiteboard to visualize the pointer swaps step-by-step. 5. Analyze time and space complexity aloud, confirming O(n) time and O(1) space, then offer the recursive alternative as a secondary thought if asked.

Key Points to Cover

  • Explicitly handling null and single-node edge cases before writing code
  • Choosing the iterative solution to demonstrate O(1) space complexity awareness
  • Using a clear three-pointer strategy (prev, curr, next) to manage state
  • Manually tracing the pointer changes with a concrete example during explanation
  • Verifying time and space complexity after presenting the solution

Sample Answer

To reverse a singly linked list efficiently, I would use an iterative approach with constant space complexity. First, I need to handle edge cases where the head is null or contains only one node, returning it immediately as no reversal is needed. For the general case, I will maintain three pointers: previous, current, and next. Initially, previous is set to null, and current points to the head. In each iteration, I save the next node to avoid losing the rest of the list. Then, I reverse the link by setting current.next to previous. After updating the links, I advance both previous and current one step forward. This process repeats until current becomes null, meaning we have reached the end of the original list. At this point, previous will be pointing to the new head of the reversed list. For example, reversing 1->2->3 starts with prev=null, curr=1. We save next=2, set 1.next to null, move prev to 1 and curr to 2. Next, we save next=3, set 2.next to 1, move prev to 2 and curr to 3. Finally, 3.next becomes 2, and we return 3 as the new head. This ensures we traverse the list exactly once, achieving O(n) time complexity while using only O(1) extra space, which aligns well with Google's emphasis on resource efficiency and clean code.

Common Mistakes to Avoid

  • Forgetting to update the next pointer before changing the current node's reference, causing the list to break
  • Failing to check for an empty list or single-node list at the very beginning
  • Attempting to swap values instead of swapping node references, which is incorrect for linked list structures
  • Not mentioning space complexity or choosing a recursive solution without discussing stack overflow risks

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 154 Data Structures questionsBrowse all 87 Google questions