Reverse Linked List
Easy · Linked Lists · Linked List · Recursion
Problem
Given the head of a singly linked list, reverse it and return the new head.
The node type and two helpers are already defined for you:
Examples
Input: head = 1 -> 2 -> 3 -> 4 -> 5 Output: 5 -> 4 -> 3 -> 2 -> 1
Input: head = None Output: None
An empty list reverses to an empty list.
Constraints
- 0 <= number of nodes <= 5000
- -5000 <= Node.val <= 5000
Approaches
- Recursive — time O(n), space O(n)
Follow-up
A linked list can be reversed either iteratively or recursively. Could you implement both?
Related problems
Start free on Cariara · Coding problems · System design studio