Skip to main content

206. Reverse Linked List

easyAsked at Anduril

Reverse a singly linked list in-place. Anduril asks this to verify you can manipulate pointer-based data structures cleanly — a skill that transfers directly to managing sensor-data pipelines and ring-buffer node chains in embedded firmware.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Anduril loops.

  • Glassdoor (2026-Q1)Mentioned in Anduril SWE onsite prep threads as a pointer-manipulation baseline.
  • Blind (2025-11)Anduril firmware and systems role discussions cite linked-list reversal as a common early-round filter.

Problem

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

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]

Explanation: All next pointers are reversed; the old tail (5) becomes the new head.

Example 2

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

Explanation: Simple two-node reversal.

Example 3

Input
head = []
Output
[]

Explanation: Empty list stays empty.

Approaches

1. Iterative (three-pointer)

Walk the list with prev, curr, and next pointers. At each step, reverse the link and advance all three.

Time
O(n)
Space
O(1)
function reverseList(head) {
  let prev = null;
  let curr = head;
  while (curr !== null) {
    const next = curr.next; // save next before overwriting
    curr.next = prev;       // reverse link
    prev = curr;            // advance prev
    curr = next;            // advance curr
  }
  return prev; // prev is the new head
}

Tradeoff: O(1) extra space. The three-pointer dance is the canonical approach and is what most Anduril interviewers expect. Nail the order of operations: save next, reverse, advance.

2. Recursive

Recurse to the end of the list, then on the way back rewire each node's next pointer.

Time
O(n)
Space
O(n) call stack
function reverseList(head) {
  if (head === null || head.next === null) return head;
  const newHead = reverseList(head.next);
  head.next.next = head; // the next node points back to head
  head.next = null;      // head's forward link is cut
  return newHead;
}

Tradeoff: Elegant but uses O(n) stack space. In embedded contexts Anduril engineers flag this — stack depth is bounded and predictable, which matters. Mention the tradeoff proactively.

Anduril-specific tips

Anduril interviewers, especially on embedded and firmware teams, care about stack depth and memory predictability. Prefer the iterative solution and explicitly state it uses O(1) extra space. Draw the pointer diagram on paper or a whiteboard before coding — verbalizing 'I save next before overwriting to avoid losing the rest of the list' demonstrates careful, methodical thinking that aligns with how Anduril engineers reason about hardware-constrained code.

Common mistakes

  • Forgetting to save curr.next before overwriting curr.next = prev — this loses the rest of the list.
  • Returning curr instead of prev at the end — curr is null after the loop; prev points to the new head.
  • Not handling the empty-list (head === null) edge case explicitly.
  • In the recursive version, forgetting to set head.next = null — this leaves a cycle at the old tail.

Follow-up questions

An interviewer at Anduril may pivot to one of these next:

  • Reverse a linked list in groups of k (LC 25 — Reverse Nodes in k-Group).
  • How would you detect if reversing in-place is safe in a concurrent system where another thread is reading the list?
  • How would you implement this on a doubly-linked list?

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Why does the iterative solution return prev instead of curr?

After the loop exits, curr is null (we walked off the end). prev points to the last real node we processed, which is the new head.

Is the recursive solution acceptable in an Anduril interview?

Usually yes in a coding context, but mention the O(n) call-stack cost. Anduril engineers in embedded roles will note that unbounded recursion is dangerous on systems with small stacks.

What happens on a list with one node?

The base case returns head immediately. For iterative: the loop runs once, sets prev = head, curr = null, returns prev (the single node) with next = null. Both are correct.

Practice these live with InterviewChamp.AI

Drill Reverse Linked List and other Anduril interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →