Skip to main content

15. Reverse Linked List

easyAsked at Brex

Reverse a singly linked list iteratively or recursively — a fundamental pointer-manipulation problem that Brex uses as a warm-up in onsite coding screens.

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

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 [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]

Approaches

1. Recursive

Recurse to the tail and rewire pointers on the way back up.

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

Tradeoff:

2. Iterative three-pointer

Walk the list once with prev/curr/next pointers, flipping each next pointer in place. O(1) space is required for production code.

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

Tradeoff:

Brex-specific tips

Brex asks about fintech infrastructure, multi-currency handling, and spend management algorithms. Expect LeetCode-style DSA focused on hash maps, sorting, and dynamic programming.

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 Brex interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →