Skip to main content

6. Reverse Linked List

easyAsked at Autodesk

Reverse a singly linked list and return the new head.

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. Solve it iteratively or recursively.

Constraints

  • 0 <= node count <= 5000
  • -5000 <= node value <= 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 collect

Push nodes onto a stack, then pop to rebuild.

Time
O(n)
Space
O(n)
const stack=[]; let p=head; while(p){stack.push(p);p=p.next;}
const dummy={next:null}; let t=dummy;
while(stack.length){t.next=stack.pop();t=t.next;} t.next=null;
return dummy.next;

Tradeoff:

2. In-place pointer flip

Walk the list once and flip each next pointer to the previous node. Constant extra space.

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

Tradeoff:

Autodesk-specific tips

Autodesk frequently reverses traversal direction over polyline/edge chains, so the in-place pointer flip pattern shows up in CAD geometry pipelines.

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

Practice these live with InterviewChamp.AI →