Skip to main content

29. Reverse Linked List

easyAsked at Snowflake

Reverse a singly linked list. Snowflake asks this as a pointer-manipulation warm-up and to verify that you can handle the three-pointer dance — the same kind of bookkeeping required when reversing scan direction inside their executor.

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

Source citations

Public interview reports confirming this problem appears in Snowflake loops.

  • Glassdoor (2026-Q1)Snowflake new-grad onsite warm-up.
  • LeetCode Discuss (2025-11)Recurring at Snowflake intern screens.

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 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. Push to stack, rebuild

Push every node onto a stack, then pop to rebuild.

Time
O(n)
Space
O(n)
function reverseList(head) {
  const stack = [];
  let curr = head;
  while (curr) { stack.push(curr); curr = curr.next; }
  const dummy = { next: null };
  let tail = dummy;
  while (stack.length) {
    tail.next = stack.pop();
    tail = tail.next;
  }
  tail.next = null;
  return dummy.next;
}

Tradeoff: Linear time, O(n) space. Mention only as the obvious-but-suboptimal version.

2. Three-pointer iterative (optimal)

Maintain prev, curr, next. At each step, flip curr.next to prev; advance all three.

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

Tradeoff: O(1) space. The trick is saving curr.next before overwriting it.

Snowflake-specific tips

Snowflake interviewers want the iterative version and to hear you narrate the three-pointer dance — most candidates get confused without explicitly naming prev/curr/next. Bonus signal: write the recursive version too and discuss stack-overflow risk for 5000-node lists.

Common mistakes

  • Forgetting to save curr.next before overwriting it — list is corrupted.
  • Returning head at the end instead of prev — head still points to the old first (now last) node.
  • Starting with prev = head instead of prev = null — first node ends up pointing to itself.

Follow-up questions

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

  • Reverse Linked List II (LC 92) — reverse a sub-range.
  • Reverse in groups of k (LC 25).
  • Recursive version — what's the stack depth risk?

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 save next before flipping?

Once you do curr.next = prev, you've overwritten the forward pointer. If you didn't save next first, you've lost access to the rest of the list.

Recursive or iterative?

Iterative for safety on long lists. Recursive is elegant but blows the stack on 5000-node inputs.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →