Skip to main content

12. Reverse Linked List

easyAsked at Grab

Reverse a singly linked list — Grab uses this as a pointer-manipulation warm-up.

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

Problem

Given the head of a singly linked list, reverse the list and return the new head. Solve iteratively in O(1) extra space.

Constraints

  • 0 <= number of nodes <= 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]

Approaches

1. Array dump and rebuild

Collect values into an array, reverse, rebuild a new list.

Time
O(n)
Space
O(n)
const arr = [];
let cur = head;
while (cur) { arr.push(cur.val); cur = cur.next; }
arr.reverse();
// rebuild list from arr

Tradeoff:

2. Three-pointer in-place

Track prev, current, next; flip current.next each step.

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

Tradeoff:

Grab-specific tips

Grab interviewers grade whether you keep three pointers straight without scratch paper — frame as reversing a ride-trace breadcrumb chain.

Solve it now

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

Output

Press Run or Cmd+Enter to execute

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →