Reverse a singly linked list and return the head
Asked in the Face to Face 1 stage.
Implement the function below. The input is the head node of a singly linked list, where each node has val and next fields. Reverse the links in place and return the new head. The evaluator represents linked lists as arrays of values when displaying inputs and outputs.
Function: def reverse_list(head):
Input: A linked-list head node, or None for an empty list.
Output: The head node of the reversed list.
Examples: [1, 2, 3, 4] becomes [4, 3, 2, 1]; [] remains [].
Constraints: Up to 10^4 nodes, integer node values, and no cycles.
def reverse_list(head):