Skip to main content

8. Reverse Linked List

easyAsked at Snap

Reverse a singly-linked list in place. Snap uses this to verify pointer-juggling fluency and ability to articulate both iterative and recursive solutions.

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

Source citations

Public interview reports confirming this problem appears in Snap loops.

  • Glassdoor (2026-Q1)Snap reports include this as a 10-minute screen problem.
  • Blind (2025-11)Mentioned alongside cycle-detection problems at Snap.

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 = []
Output
[]

Approaches

1. Stack-based

Push all nodes onto a stack, then pop them into a new list.

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

Tradeoff: Linear extra space. Works but the iterative pointer flip is strictly better.

2. Three-pointer iterative flip (optimal)

Maintain prev, curr, next. For each node, save next, flip curr.next to prev, advance prev and curr.

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

Tradeoff: O(1) extra space. The 'save next first' step is the crux — without it you lose the rest of the list.

Snap-specific tips

At Snap, candidates who can sketch both iterative AND recursive versions earn higher signal — recursion shows you understand the stack model. Bonus: relate this to reversing a Snap conversation thread for replay, where reversing in-place avoids re-renders.

Common mistakes

  • Forgetting to save next before flipping — loses the tail.
  • Returning curr (which is null) instead of prev (the new head).
  • Mutating during traversal without a temp variable for next.

Follow-up questions

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

  • Reverse a sublist between positions m and n (LC 92).
  • Reverse nodes in k-groups (LC 25).
  • Detect if a list is a palindrome using reversal of the second half.

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Is recursion cleaner or more error-prone here?

Recursive code is shorter but uses O(n) stack. For 5000-node inputs it's safe; for arbitrary depths the iterative version is safer in production.

Does this work on doubly-linked lists?

Almost — you also need to flip the prev pointer at each step. The skeleton is the same.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →