Skip to main content

206. Reverse Linked List

easyAsked at Hugging Face

Reverse a singly linked list in-place. Hugging Face asks this to confirm pointer manipulation fluency — a prerequisite for understanding the attention mask and key-value cache pointer operations that arise in transformer serving optimizations.

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

Source citations

Public interview reports confirming this problem appears in Hugging Face loops.

  • Glassdoor (2026-Q1)Appears in Hugging Face phone screen reports as a fundamental data structures test.
  • Blind (2025-11)Hugging Face interview threads note Reverse Linked List as a common early-round check for pointer reasoning.

Problem

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

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: All pointers are flipped; the old tail becomes the new head.

Example 2

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

Explanation: Two-node reversal.

Example 3

Input
head = []
Output
[]

Explanation: Empty list stays empty.

Approaches

1. Iterative (three-pointer)

Walk the list maintaining prev, curr, and next pointers. At each step, flip the next pointer of curr to point to prev, then 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;
    curr.next = prev;
    prev = curr;
    curr = next;
  }
  return prev;
}

Tradeoff: O(1) space — the expected production answer. Clean and easy to trace through with a three-element example on a whiteboard.

2. Recursive

Recurse to the tail, then on the way back flip 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;
  head.next = null;
  return newHead;
}

Tradeoff: Elegant but uses O(n) stack space — a problem for very long lists (e.g., a linked list of model config tokens). Always mention the iterative version as the preferred solution in production.

Hugging Face-specific tips

Hugging Face appreciates connecting the problem to their domain: 'This is the same pointer-swap pattern used when reversing token sequences or flipping attention masks for bidirectional encoding.' Trace through a three-node example explicitly — it demonstrates that you can reason about pointer state without running code, which is a key remote-collaboration skill valued by a distributed team.

Common mistakes

  • Losing the reference to next before flipping — always save next = curr.next before reassigning curr.next.
  • Returning curr instead of prev — at loop exit curr is null; prev points to the new head.
  • Not handling the empty list or single-node list edge cases.
  • In the recursive approach, forgetting to set head.next = null — this creates a cycle in the list.

Follow-up questions

An interviewer at Hugging Face may pivot to one of these next:

  • Reverse Linked List II (LC 92) — reverse only a subrange [left, right].
  • Reverse Nodes in k-Group (LC 25) — reverse in chunks of k.
  • How would you reverse a doubly linked list? What additional pointer needs updating?

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 approach use O(1) space while recursive uses O(n)?

The iterative version only stores three pointers. The recursive version keeps a stack frame for every node until it reaches the tail.

What does prev start as, and why null?

null becomes the new next of the original head (now the tail), correctly terminating the reversed list.

Can I collect nodes into an array and rebuild?

Yes — O(n) space, O(n) time — but it defeats the point of the problem and won't be accepted if the interviewer wants an in-place solution.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →