Problem
Reverse a singly linked list, both iteratively and recursively.
Input / Output
- Input: head of a singly linked list.
- Output: the head of the reversed list.
Constraints
- 0 <= nodes <= 5000; the iterative version should use O(1) extra space.
Example