Skip to main content

206. Reverse Linked List

easyAsked at DRW

DRW asks Reverse Linked List to test pointer manipulation under pressure — a skill that maps to low-level ring-buffer and lock-free queue implementations used in market-data feed processing.

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

Source citations

Public interview reports confirming this problem appears in DRW loops.

  • Glassdoor (2026-Q1)DRW SWE candidates report linked-list pointer manipulation as a recurring theme in early technical screens.
  • Blind (2025-11)DRW threads note that pointer-reversal questions are used to assess low-level memory reasoning, which the firm values for systems-programming 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: Nodes are rewired in reverse order.

Example 2

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

Explanation: Two-node reversal.

Example 3

Input
head = []
Output
[]

Explanation: Empty list returns null.

Approaches

1. Iterative (three-pointer)

Walk the list once, keeping track of the previous node, the current node, and the next node. Rewire current.next to point backward on each step.

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

Tradeoff: O(n) time, O(1) space — the preferred answer at DRW. No recursion overhead, cache-friendly, and directly analogous to in-place buffer manipulation.

2. Recursive

Recurse to the end of the list, then rewire links on the way back up. The recursion depth is O(n), so the call stack uses O(n) space.

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 concern at DRW where large lists and tight memory budgets coexist. Present iterative first.

DRW-specific tips

DRW interviewers care about the iterative version's O(1) space property more than elegance. After coding it, they will ask: 'How would you reverse a doubly-linked list in-place?' (swap next and prev pointers for each node). They may also ask about reversing a sublist — LC 92 — which is a direct extension used in order-book manipulation. Mention that lock-free queues in C++ use similar pointer-swap patterns.

Common mistakes

  • Losing the forward reference by setting curr.next = prev before saving next — always save next before rewiring.
  • Returning curr instead of prev at the end — curr is null after the loop; prev holds the new head.
  • Not handling null and single-node inputs — both should return head unchanged.
  • Choosing recursion without flagging the O(n) stack-space cost — DRW will note it even if you don't.

Follow-up questions

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

  • Reverse Linked List II (LC 92) — reverse only nodes from position left to right.
  • How would you reverse a doubly-linked list in O(n) time and O(1) space?
  • If nodes arrive on a live stream one at a time, how would you reverse the accumulating list incrementally?

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 DRW prefer the iterative version?

The O(1) space property matters in systems where heap allocations are minimized. The iterative version also maps more directly to low-level pointer manipulation in C++ ring buffers.

What is the biggest mistake candidates make?

Forgetting to save next before overwriting curr.next. Draw the three pointers explicitly before coding — DRW interviewers often ask you to trace through on a 3-node example.

How does this relate to DRW's infrastructure?

Market-data feed decoders often process messages from a ring buffer or intrusive linked list. In-place reversal without allocation is a real technique in these systems.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →