The iterative version I could do in my sleep, three pointers, walk the list, done.
Start by clarifying the problem and edge cases, then explain the iterative approach using three pointers (prev, curr, next) to reverse the links in one pass. Next, describe the recursive approach where you recursively reverse the rest of the list and adjust pointers, emphasizing the base case and call stack. Finally, compare time and space complexity and discuss trade-offs.
Pro tip: Mention that the recursive solution uses O(n) stack space, which can cause stack overflow for large lists, so iterative is often preferred in production. Also, write clean code with meaningful variable names and handle edge cases like empty list and single node upfront.
Confirm the problem: reverse a singly linked list and return new head. Ask about constraints (e.g., list size, memory limits) and handle edge cases: empty list, single node, two nodes.
Explain using three pointers: prev (initially null), curr (head), and next. Iterate while curr is not null, reversing the link and advancing pointers. Return prev as new head.
Define base case: if head is null or head.next is null, return head. Recursively reverse the rest, then set head.next.next = head and head.next = null. Return new head from recursion.
State time complexity O(n) for both. Space: iterative O(1), recursive O(n) due to call stack. Discuss trade-offs and when to use each.
Walk through an example (e.g., 1->2->3->null) for both approaches, verifying pointer changes. Mention testing edge cases and potential pitfalls like losing references.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.