Linked list, back to front
Write reverse_list(head): given the first node of a singly linked list (or None for an empty list), reverse the list and return its new first node. Each ListNode has val and next.
Re-link the existing nodes instead of copying values: one pass, O(1) extra memory.
The tests call the provided run_reverse_list(values), which builds the list from values and returns the values of the list you return.
Examples
run_reverse_list([1,2,3,4,5])→[5,4,3,2,1]run_reverse_list([4,9])→[9,4]run_reverse_list([])→[]