Skip to main content

10. Reverse Linked List

easyAsked at Freshworks

Reverse a singly-linked list iteratively — the warm-up Freshworks asks before any deeper linked-list problem like ticket-queue rotation.

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.

Constraints

  • 0 <= list length <= 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 = []
Output
[]

Approaches

1. Brute force (array buffer)

Push all values into an array, then rebuild a new list in reverse order.

Time
O(n)
Space
O(n)
const vals = [];
let cur = head;
while (cur) { vals.push(cur.val); cur = cur.next; }
let newHead = null;
for (const v of vals) newHead = { val: v, next: newHead };
return newHead;

Tradeoff:

2. Three-pointer iterative

Walk forward keeping prev, curr, next. Flip curr.next to prev each step. Return prev when done.

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:

Freshworks-specific tips

Freshworks always asks the recursive variant as a follow-up — sketch the stack diagram before coding so you don't lose them on the off-by-one when head.next.next = head.

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

Practice these live with InterviewChamp.AI →