Skip to main content

206. Reverse Linked List

easyAsked at HP

HP's systems software and firmware teams deal with linked structures constantly — job queues, device-descriptor chains, and print-spooler task lists. Reversing a linked list in-place is a fundamental pointer-manipulation exercise that HP uses to probe candidate comfort with low-level memory operations.

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

Source citations

Public interview reports confirming this problem appears in HP loops.

  • Glassdoor (2026-Q1)Mentioned in HP SWE onsite feedback as a standard data-structures question across multiple rounds.
  • Blind (2025-12)HP preparation threads include Reverse Linked List as a core pointer-manipulation problem tested in systems-software roles.

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 in 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: The list is reversed in-place; each node's next pointer now points to its predecessor.

Example 2

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

Explanation: Single edge reversal.

Example 3

Input
head = []
Output
[]

Explanation: Empty list returns null.

Approaches

1. Iterative (three-pointer)

Walk the list with three pointers: prev (null), curr (head), and next. At each step, redirect curr.next to prev, then advance all three pointers.

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

Tradeoff: O(1) space — preferred in systems contexts where stack depth is constrained. No risk of stack overflow for long lists.

2. Recursive

Recurse to the end of the list, then rewire pointers on the way back up.

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 node after head now points back to head
  head.next = null;      // head becomes the tail
  return newHead;
}

Tradeoff: Elegant but uses O(n) call-stack space. For embedded or firmware contexts HP favors the iterative version due to stack-size limits.

HP-specific tips

HP systems interviewers pay close attention to pointer discipline — draw the before/after diagram on the whiteboard before writing code. Explicitly save curr.next before overwriting it; forgetting this is the classic mistake. Mention that in a firmware context you'd prefer the iterative version to avoid stack overflow on long device-descriptor chains. Be ready to code both approaches.

Common mistakes

  • Overwriting curr.next before saving it — you lose the rest of the list.
  • Returning curr instead of prev at the end — curr is null when the loop exits; prev holds the new head.
  • Not handling the empty list or single-node list — both are valid inputs and must return gracefully.
  • In the recursive version, forgetting to set head.next = null — the original head becomes a cycle.

Follow-up questions

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

  • Reverse only a sub-portion of the list from position left to right (LC 92).
  • Reverse the list in groups of k nodes at a time (LC 25).
  • How does your approach change if the list is doubly-linked?

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 is iterative preferred over recursive in production?

Recursive reversal uses O(n) call-stack frames. For large lists (or in embedded environments with limited stack), this risks a stack overflow. Iterative runs in O(1) space.

What does prev represent at each step?

prev is the already-reversed portion of the list. At the end of the loop, prev is the new head of the fully reversed list.

How do you handle a null input?

The iterative loop condition curr !== null handles it — if head is null, curr starts null and the loop body never runs; prev remains null, which is the correct return value.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →