Skip to main content

206. Reverse Linked List

easyAsked at Elastic

Reverse a singly-linked list in place. Elastic uses pointer-manipulation questions like this to verify that candidates can reason about mutable state without a garbage-collection safety net — critical intuition for writing correct segment merging and linked-structure traversal in distributed search engines.

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

Source citations

Public interview reports confirming this problem appears in Elastic loops.

  • Glassdoor (2026-Q1)Elastic SWE candidates report linked-list reversal as a common early-round question testing pointer reasoning.
  • Blind (2025-11)Elastic phone-screen threads mention the iterative linked-list reversal as a baseline check before graph and trie questions.

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: Each node's next pointer is redirected to its predecessor.

Example 2

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

Explanation: Two-node list reversed.

Example 3

Input
head = []
Output
[]

Explanation: Empty list returns null.

Approaches

1. Iterative — three-pointer

Walk the list once, maintaining prev, curr, and next pointers. 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 next before overwriting
    curr.next = prev;       // reverse the pointer
    prev = curr;            // advance prev
    curr = next;            // advance curr
  }
  return prev; // prev is new head
}

Tradeoff: O(n) time, O(1) space — optimal. No extra memory, single pass. Preferred by Elastic interviewers who value in-place mutations for large data structures.

2. Recursive

Recursively reverse the rest of the list, then append the current node at the end of the reversed sublist.

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; // append current node at end of reversed tail
  head.next = null;      // sever the old forward link
  return newHead;
}

Tradeoff: Elegant but uses O(n) stack space — risks stack overflow on lists with 5000+ nodes. Fine to present as an alternative but always default to iterative in production systems.

Elastic-specific tips

State aloud that you are saving next before overwriting curr.next — Elastic interviewers want to see that you understand why losing the forward reference is the primary off-by-one risk. Draw the three-pointer diagram on the virtual whiteboard before writing code. For the follow-up, be ready to reverse only a subrange [left, right] (LC 92) — Elastic may escalate to that variant.

Common mistakes

  • Not saving curr.next before overwriting it — the forward reference is lost and the rest of the list becomes unreachable.
  • Returning curr instead of prev at the end — curr is null when the loop exits; prev is the new head.
  • Forgetting the null-check for an empty list — return head (which is null) immediately if head is null.
  • Using the recursive version without mentioning stack-overflow risk on large inputs.

Follow-up questions

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

  • Reverse Linked List II (LC 92) — reverse only nodes from position left to right.
  • Palindrome Linked List (LC 234) — reverse the second half and compare.
  • How would you reverse a doubly-linked list in place?

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 must next be saved before redirecting curr.next?

After curr.next = prev, the original forward link is gone. Without saving it first, you lose access to the rest of the list.

Why does prev start as null?

The original head node becomes the new tail, so its next pointer must point to null. Initializing prev = null achieves this automatically on the first iteration.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →