Skip to main content

206. Reverse Linked List

easyAsked at JPMorgan

Reverse a singly linked list. JPMorgan asks this on Software Engineer Programme phone screens to verify you can manipulate pointers safely without losing the next reference — a foundational signal before any harder linked-list follow-up.

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

Source citations

Public interview reports confirming this problem appears in JPMorgan loops.

  • Glassdoor (2026-Q1)Recurring JPMorgan SDE phone-screen warm-up in new-grad reviews.
  • LeetCode (2026-Q1)Tagged JPMorgan on the LeetCode company tag page.
  • Reddit r/cscareerquestions (2025-10)JPMC SWE-I write-up: 'first question, reverse a linked list, then add two numbers as a follow-up'.

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]

Example 2

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

Example 3

Input
head = []
Output
[]

Approaches

1. Iterative three-pointer (optimal)

Walk the list with prev, cur, next. On each step, store cur.next, flip cur.next to prev, advance prev and cur. New head is the final prev.

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

Tradeoff: O(n) time, O(1) extra space — the production-grade answer. The only state you need is the previous node; everything else is recomputable from cur.next.

2. Recursive

Recurse to the tail, then on the way back up reverse each link by writing head.next.next = head and head.next = null.

Time
O(n)
Space
O(n) call stack
function reverseListRec(head) {
  if (head === null || head.next === null) return head;
  const newHead = reverseListRec(head.next);
  head.next.next = head;
  head.next = null;
  return newHead;
}

Tradeoff: Same time but O(n) call-stack space. Acceptable on a phone screen if you also articulate the iterative version. Avoid in production: a 5000-node list is still within Node's default stack but the iterative version is strictly safer.

JPMorgan-specific tips

JPMorgan phone-screen interviewers grade three things: (1) you draw the three-pointer dance on a whiteboard or in comments before coding; (2) you handle the empty-list and single-node cases without a special branch (the loop naturally does); (3) you return prev, not head, at the end. The classic bug is returning head — head is the original tail after the loop and its next is null.

Common mistakes

  • Returning head at the end — the original head now sits at the tail with .next = null.
  • Forgetting to save cur.next before overwriting cur.next = prev — loses the rest of the list.
  • Initialising prev = head instead of null — creates a cycle on the first pass.
  • Using recursion without thinking through the stack-depth implication on long lists.

Follow-up questions

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

  • Reverse a linked list between positions m and n (LC 92).
  • Reverse nodes in k-group (LC 25).
  • Palindrome linked list (LC 234) — reverse the second half, then two-pointer compare.
  • Detect if a linked list has a cycle (LC 141).
  • Swap nodes in pairs (LC 24).

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 the recursive solution ever preferred?

Only when the interviewer explicitly asks for it or when the surrounding problem (e.g. reverse-nodes-in-k-group) is naturally recursive. The iterative version is the default — O(1) space and never blows the call stack.

What is the worst-case bug on this problem?

Forgetting to save cur.next before overwriting it. The instant you assign cur.next = prev, you have lost the rest of the list and the loop terminates with a wrong answer (only the first node reversed). The four lines inside the while must happen in exactly the order shown.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →