Skip to main content

206. Reverse Linked List

easyAsked at Cohere

Reverse a singly-linked list in place. Cohere interviewers use this to probe pointer manipulation — the same discipline required when implementing custom tokenizer chains and streaming decoder buffers.

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

Source citations

Public interview reports confirming this problem appears in Cohere loops.

  • Glassdoor (2026-Q1)Cohere SWE candidates report linked-list reversal as a recurring phone-screen problem.
  • Blind (2025-12)Listed in a Cohere interview prep thread as a must-know easy problem for SWE 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: Each pointer is reversed so 5 becomes the new head.

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 maintaining prev, curr, and next pointers. At each step reverse the curr.next pointer, then advance all three.

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

Tradeoff: O(n) time, O(1) space — ideal. No call-stack overhead. Most interviewers expect this approach first.

2. Recursive

Recurse to the end of the list, then fix pointers on the way back up.

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; // point next node back at current
  head.next = null;      // break forward pointer
  return newHead;
}

Tradeoff: Elegant but uses O(n) stack space — problematic for very long lists or memory-constrained inference servers. Mention iterative as preferred in production.

Cohere-specific tips

Cohere may ask you to reverse only a sub-range of a linked list (LC 92 — Reverse Linked List II). Practicing the three-pointer pattern makes that extension straightforward. Mention the analogy to reversing a sequence of token IDs in a sliding window — Cohere engineers think about sequences constantly.

Common mistakes

  • Losing the reference to next before reversing — always save next = curr.next before modifying curr.next.
  • Returning curr instead of prev — at loop end curr is null; prev is the new head.
  • Not handling the empty list — check head === null before the loop.
  • Forgetting to set head.next = null in the recursive version — creates a cycle.

Follow-up questions

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

  • Reverse Linked List II — reverse only a sub-range [left, right] in one pass.
  • Palindrome Linked List — reverse the second half and compare.
  • How would you reverse a doubly-linked list?

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 the iterative approach use O(1) space?

It only maintains three pointer variables regardless of list length. No auxiliary data structure or call stack is involved.

When would you prefer the recursive approach?

When code clarity matters more than stack depth — e.g., in test code or when the list is guaranteed short. In production inference systems, iterative is safer.

Can you reverse in place without a prev pointer?

Not cleanly. You could collect values into an array and rewrite them, but that is O(n) space and not truly in-place pointer reversal.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →