Skip to content
AI360Xpert
Beta
Difficulty: EasyLinked List

Reverse Linked List

Problem in Plain English

Given the head of a singly linked list, flip every next pointer so the list runs in the opposite order, and return the new head.

Problem Statement

Given the head of an acyclic singly linked list, reverse the direction of every next link and return the new head. An empty list returns null.

Reuse every original node and preserve its value. The original tail becomes the new head, the original head becomes the null-terminated tail, and the caller must use the returned head to traverse the complete result.

Constraints

  • The number of nodes satisfies 0≤n≤50000 \le n \le 5000.
  • Each node value satisfies −5000≤Node.val≤5000-5000 \le \mathrm{Node.val} \le 5000.
  • The input is acyclic; preserve the original node objects and values while reversing their links.

Examples

Example 1

Input
head = [1,2,3,4,5]
Output
[5,4,3,2,1]

Reverse all four original links and terminate node 1 with null. Return the original node 5, so traversing from the returned head visits the original nodes in reverse order.

Example 2

Input
head = [1,2]
Output
[2,1]

Point node 2 to node 1 and clear node 1 next. Returning the old head would expose only node 1 rather than both nodes.

Example 3

Input
head = []
Output
[]

There is no node or link to change. Both approaches return null immediately without allocating an output node.

Example 4

Input
head = [4]
Output
[4]

The sole node is already the head and null-terminated tail. The returned reference is the same object.

Example 5

Input
head = [2,2,-1,2]
Output
[2,-1,2,2]

The original last node comes first even though several values are equal. Correct reversal changes node order and links while preserving each node value.

Intuition

A node cannot point backward until we remember the node before it. For Example 1, save the link from 1 to 2, point 1 at null, then save the link from 2 to 3 and point 2 at 1. This grows a reversed prefix while keeping the untouched suffix reachable. Recursion reaches the old tail first and performs the same link repair while calls return; iteration stores the needed references directly and avoids a call stack.

Approaches

Recursive Reversal

Solution Details

Reveal Recursive Reversal: intuition, complexity, and code

Hints

Hint 1
Track the node behind you with a prev pointer as you walk forward.
Hint 2
Save curr.next before overwriting it, or the rest of the list becomes unreachable.
Hint 3
Recursively, assume the suffix is already flipped and only repair the link between the head and its successor.

Edge Cases

  • An empty list returns null, and a singleton returns the same node with its null next link.
  • A two-node list needs both the backward link and a null old-head link; omitting the latter makes a cycle.
  • Negative or repeated values do not affect reversal, which is defined by node position and identity.
  • At 5000 nodes, recursion uses 5000 nested frames. The Python wrapper adjusts its limit, but Java and JavaScript stack capacity remains runtime-dependent; the iterative method needs no stack adjustment.
  • Any other reference to an input node observes changed links after this in-place operation. An old head reference now identifies only the new tail.

Common Mistakes and Interview Tips

  • Overwriting curr.next before saving its successor loses access to the untouched suffix.
  • Returning curr after the iterative loop returns null; return prev and assign that returned head in the caller.
  • In recursion, attaching head backward without clearing its old next creates a cycle between head and its original successor.
  • Calling recursion a constant-space method because no nodes are allocated ignores the linear call stack.
  • Reversing only values or constructing copies does not preserve the required original-node ordering; validate object references when values repeat.
  • Explain the reversed-prefix invariant in an interview and choose iteration for large or unknown depths rather than relying on a language default stack limit.

Key Takeaway

List rewiring comes down to saving the next link before overwriting it; that one habit prevents almost every lost-node bug.