Skip to main content

11. Reverse Linked List

easyAsked at Flipkart

Reverse a singly linked list iteratively and recursively — Flipkart uses it to confirm pointer hygiene before moving on to LRU-cache style problems.

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 it both iteratively and recursively if asked.

Constraints

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

Approaches

1. Stack reversal

Push all nodes onto a stack and pop to rebuild the list.

Time
O(n)
Space
O(n)
// push every node, then rewire next pointers as you pop

Tradeoff:

2. Three-pointer iteration

Walk forward keeping prev, curr, next; rewire curr.next to prev each step. O(1) extra space.

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:

Flipkart-specific tips

Flipkart interviewers expect a clean iterative answer first, then a 30-second sketch of the recursive variant — sketching both signals you know the call-stack trade-off.

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

Practice these live with InterviewChamp.AI →