🚀 AI-Powered Mock Interviews Launching Soon - Join the Waitlist for Early Access

technicalmedium

Given a singly linked list, write a function to reverse it in-place. Your solution should achieve O(n) time complexity and O(1) space complexity.

technical screen · 10-15 minutes

How to structure your answer

The optimal approach for in-place linked list reversal with O(n) time and O(1) space complexity involves an iterative three-pointer technique. Initialize prev to null, current to the head of the list, and next_node to null. Iterate through the list using a while loop as long as current is not null. Inside the loop, first, store current.next in next_node to avoid losing the rest of the list. Second, reverse the current node's pointer by setting current.next = prev. Third, advance prev to current. Finally, advance current to next_node. After the loop terminates, prev will point to the new head of the reversed list. This method systematically re-links each node, ensuring constant extra space and a single pass through the list.

Sample answer

To reverse a singly linked list in-place with O(n) time and O(1) space complexity, an iterative approach using three pointers is most effective. We initialize prev to null, current to the head of the list, and next_node to null. The core logic resides within a while loop that continues as long as current is not null. Inside the loop, the first step is to temporarily store current.next in next_node. This is crucial because current.next will be modified in the next step, and we need to retain access to the rest of the original list. Second, we reverse the pointer of the current node by setting current.next = prev. Third, we advance prev to current, effectively adding the current node to the reversed portion of the list. Finally, we advance current to next_node to move to the next element in the original list. Once the loop finishes, prev will be pointing to the last node of the original list, which is now the new head of the reversed list. This method ensures each node is visited once and only a constant number of extra pointers are used.

Key points to mention

  • • Understanding of pointer manipulation in linked lists.
  • • Ability to achieve O(n) time complexity by traversing the list once.
  • • Ability to achieve O(1) space complexity by using a constant number of pointers.
  • • Correct handling of edge cases: empty list, single-node list.
  • • The iterative approach is preferred for space complexity over a recursive solution (which would use O(n) stack space).

Common mistakes to avoid

  • ✗ Losing track of the `next` node before reversing the current node's pointer, leading to a disconnected list.
  • ✗ Incorrectly initializing or updating the `prev` or `current` pointers.
  • ✗ Failing to handle the `head` of the list correctly after reversal.
  • ✗ Off-by-one errors in loop conditions or pointer assignments.
  • ✗ Using recursion, which typically incurs O(n) space complexity due to the call stack, violating the O(1) space constraint.