Skip to content

visual walkthrough

Reverse Linked List

EasyIn-place ReversalReported at: AmazonMicrosoftApple+5

The single most important linked-list routine; do it iteratively and recursively.

Solve on LeetCode

The idea

Reversing a list doesn't mean moving nodes: every node stays where it is, and only its arrow turns around to point at the node before it.

Three pointers do it in one pass: prev (the reversed part), curr (the node being flipped) and next (saved first, so the rest of the list isn't lost).

Complexity

approachtimespace
Copy and write backO(n)O(n)
Flip the arrowsO(n)O(1)

More walkthroughs