Skip to main content

6. Reverse Linked List

easyAsked at ByteDance

Reverse a singly linked list in place — ByteDance uses it to verify pointer fluency before moving to feed-pipeline buffer questions.

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

Problem

Given the head of a singly linked list, reverse the list and return its new head. Solve it in-place using O(1) extra space.

Constraints

  • 0 <= n <= 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

Push all nodes onto a stack, then pop them back into a list.

Time
O(n)
Space
O(n)
const st=[]; let c=head;
while(c){st.push(c); c=c.next;}
// pop and reconnect

Tradeoff:

2. Three pointers

Walk the list and flip the next pointer of each node using prev/curr/next bookkeeping. Constant 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:

ByteDance-specific tips

ByteDance interviewers expect you to dry-run the pointer swap on a 3-node example before coding, mirroring how their video buffer team draws diagrams before edits.

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

Practice these live with InterviewChamp.AI →