Given the head of a singly linked list, reverse the list and return the new head. (Implement `reverse_list(head)` where each node has `.val` and `.next`.)
Examples
Input
head = [1,2,3,4,5]
Output
[5,4,3,2,1]
Constraints
- 0 <= list length <= 5000
Given the head of a singly linked list, reverse the list and return the new head. (Implement `reverse_list(head)` where each node has `.val` and `.next`.)
head = [1,2,3,4,5]
[5,4,3,2,1]
Explore step-by-step interactive animations, memory state tracking, and live multi-language execution in DrawCode.
Launch Interactive Visualizer