Skip to main content

206. Reverse Linked List

easyAsked at Goldman Sachs

Reverse a singly linked list iteratively and recursively. Goldman Sachs uses Reverse Linked List as the canonical 'do you understand pointer manipulation' question — they ask for both the iterative and recursive versions in the same round.

By Sam K., Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Goldman Sachs loops.

  • Glassdoor (2026-Q1)Goldman Sachs SWE candidate reports list Reverse Linked List as the most-asked linked-list question, often paired with 'now write it recursively'.
  • LeetCode Discuss (2025-11)Reverse Linked List is in the top-10 of LeetCode's Goldman Sachs company tag.

Problem

Given the head of a singly linked list, reverse the list, and return the reversed list. Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both?

Constraints

  • The number of nodes in the list is the range [0, 5000].
  • -5000 <= Node.val <= 5000

Examples

Example 1

Input
head = [1,2,3,4,5]
Output
[5,4,3,2,1]

Example 2

Input
head = [1,2]
Output
[2,1]

Example 3

Input
head = []
Output
[]

Approaches

1. Iterative with three pointers (optimal)

Walk the list with prev, cur, next pointers. Reverse each node's next pointer in place.

Time
O(n)
Space
O(1)
function reverseList(head) {
  let prev = null;
  let cur = head;
  while (cur !== null) {
    const next = cur.next;
    cur.next = prev;
    prev = cur;
    cur = next;
  }
  return prev;
}

Tradeoff: O(n) time, O(1) space. The order of operations (capture next BEFORE rewriting cur.next) is the part Goldman is grading — flip the order and the list breaks.

2. Recursive (bottom-up)

Recurse to the end; on the way back up, rewire each node's next pointer to its predecessor.

Time
O(n)
Space
O(n)
function reverseListRec(head) {
  if (head === null || head.next === null) return head;
  const newHead = reverseListRec(head.next);
  head.next.next = head;
  head.next = null;
  return newHead;
}

Tradeoff: Linear time at O(n) stack space. The 'head.next.next = head' line trips most candidates — it means 'the node after me should now point back to me'. Goldman wants you to verbalize that exact thought.

Goldman Sachs-specific tips

Goldman Sachs interviewers will almost always ask for BOTH the iterative and recursive versions on this one. Show them both even if only one is requested — it signals depth. The capture-next-first detail in the iterative version is what they're grading; reversing the order silently breaks the list and demonstrates you don't understand pointer aliasing.

Common mistakes

  • Forgetting to capture the next pointer before rewriting cur.next — leaks the rest of the list.
  • Returning head instead of prev in the iterative version — head now points to NULL at end.
  • In the recursive version, forgetting head.next = null on the last rewire, which creates a cycle.

Follow-up questions

An interviewer at Goldman Sachs may pivot to one of these next:

  • Reverse a subset: reverse nodes from position m to n (Reverse Linked List II, LC #92).
  • Reverse in groups of k (LC #25 Reverse Nodes in k-Group).
  • Detect a cycle (LC #141 Linked List Cycle) — sometimes asked back-to-back to test pointer fluency.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Which version does Goldman prefer?

Both. Senior interviewers tend to default to iterative because it's O(1) space and clearer for production code; new-grad rounds often start with recursion to test base-case fluency. Be ready with both — they take 2 minutes apiece once warmed up.

What's the most common bug?

Forgetting to capture cur.next before rewriting it. The line 'const next = cur.next' must come BEFORE 'cur.next = prev' — flip them and you immediately lose the tail of the list.

Free learning resources

Curated free links for this problem.

Companies that also ask Reverse Linked List