#206 Reverse Linked List
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
Input
head = 1 → 2 → 3 → 4 → 5Output
5 → 4 → 3 → 2 → 1Same five nodes, every arrow flipped.
Input
head = 1 → 2Output
2 → 1The old tail becomes the head you return.
Input
head = nullOutput
nullNothing 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.