#206 Reverse Linked List

EasyLinked List~12 min

Given the head of a singly linked list, reverse it in place. Every node's next pointer should end up pointing at its former predecessor, and return the new head (which is the old tail). An empty list reverses to an empty list.

Examples

Inputhead = 1 → 2 → 3 → 4 → 5
Output5 → 4 → 3 → 2 → 1

Same five nodes, every arrow flipped.

Inputhead = 1 → 2
Output2 → 1

The old tail becomes the head you return.

Inputhead = null
Outputnull

Nothing to reverse.

Constraints

  • 0 ≤ number of nodes ≤ 5000
  • -5000 ≤ Node.val ≤ 5000

The explainer is the good part.

Sign in for the full intuition build-up, every approach from naive to optimal with complexity analysis, progressive hints, and commented solutions in Python, JavaScript, and Java. All free.

Want a taste first? Try a free sample: Two Sum or Valid Parentheses.