Given the head of a linked list, the task is to reverse this list and return the reversed head.
Example 1
Input: Linked list: 10 -> 20 -> 30 -> 40 -> 50
Output: 50 -> 40 -> 30 -> 20 -> 10
Explanation: The linked list is reversed such that the last node becomes the head, and the head becomes the last node.
Example 2
Input: Linked list: -3 -> -2 -> -1 -> 0 -> 1
Output: 1 -> 0 -> -1 -> -2 -> -3
Explanation: The linked list is reversed while maintaining the relative positions of negative and positive numbers.
Sign in to write, run, and submit your solution against the full test suite.