PROBLEM 17 OF 75 · DEEP-DIVE PILOT

Reverse Linked List

Given the head of a singly linked list, reverse its next references and return the new head. Keep every original node; an empty list stays empty.

Know the exact contract

  • The local code studio represents each node as an object with val and next fields. The input is a nested chain, or null for an empty list.
  • The intended solution rewires the existing nodes using constant extra space. The practice runner checks the resulting chain, not object identity or memory usage.
EXAMPLE 11 → 2 → 3 → null→ 3 → 2 → 1 → null

Each next reference changes direction and the old tail becomes the new head.

EXAMPLE 27 → null→ 7 → null

A one-node list has no edge to reverse.

EXAMPLE 3null→ null

There is no node to process or return.

From copied values to reversed references

SIMPLE APPROACH

Read all values into an array, then build a new list in reverse order. This takes O(n) time and O(n) extra space, but does not practice the constant-space pointer rewiring the interview question is designed to test.

IMPROVED APPROACH

Keep previous and current references. Before changing current.next, save it as following. Redirect current.next to previous, move previous to current, and move current to following. When current becomes null, previous is the new head.

Why it works

Before each iteration, previous heads a correctly reversed prefix of the original list, while current heads the untouched suffix. Saving following preserves access to the suffix; redirecting one edge safely extends the reversed prefix by one node.

Cost

Each of n nodes is visited once, so the pointer-rewiring approach uses O(n) time and O(1) extra space. The returned list reuses existing nodes; no new data nodes are needed.

A tempting mistake

For 1 → 2 → 3, if you set 1.next to null before saving its original next reference, node 2 and the rest become unreachable from your current state. Save following first, then rewire.

FOLLOW THE POINTERS

Save next. Then change next.

Node numbers below identify original objects, even when values repeat. Follow the saved next reference before the arrow turns around.

STEP 1 / 4

Current: node #1 (1) · saved next: node #2

Reversed prefix: #1:1 → null

Untouched suffix: #2:2 → #3:3 → #4:4 → null

Save the next reference before rewiring node 1. Its new next points to null.
New head: node #4
GUIDED CHECK · BEFORE YOU CODE

Can you keep the suffix safe?

These choices help you inspect the reasoning. Only the separate code pass records an independent solve.

1. Why save current.next before rewiring?
2. What does previous represent before each loop?
3. After saving following, which reference changes?
4. When current becomes null, what is the returned head?
5. What is the cost of rewiring one node per iteration?
INDEPENDENT SOLVE

Rewire the chain yourself.

Write solve(head). A node has val and next, and head may be null. Return the new head after reversing the chain. The local grader compares the returned structure; it does not certify that you used the original node identities or constant memory.

Opening the code studio…

PROVE IT AGAIN TOMORROW

A fresh solve is the test.

Loading your saved attempts…

For additional practice, see the related LeetCode challenge.