A linked list is a chain of nodes. Each ListNode holds a value val and a reference next to the following node; the last node's next is None. There is no index: to reach the third node you must start at the head and follow next twice.
A toy train is stored as a singly linked list of carriages, head first. To send it back the way it came, turn every coupling around so that the old last carriage leads, and return that new front carriage. The tests build the list from a Python list with list_to_linked and convert your result back with linked_to_list.
Examples
Input: 3 -> 9 -> 4 -> 7
Output: 7 -> 4 -> 9 -> 3
Input: (empty list)
Output: (empty list)
Constraints
0 <= number of nodes <= 1000- Re-link the existing nodes; do not build a new list from the values
Goals
- Traverse a singly linked list by following next references
- Re-link nodes iteratively with prev and current references without losing the rest of the list