head. Recouple the cars so they run in the opposite order, and return the new head, which is the former last node.
An empty list (head is None) stays empty.
Try solving it two ways: with a loop and with recursion.head=list_node([7, -2, 0, 15]) → [15, 0, -2, 7]
The nodes come in reverse order: the last one (15) becomes the head and the first one (7) becomes the tail.
head=list_node([42]) → [42]
A single-node list stays the same when reversed.
head=list_node([]) → None
An empty list stays empty.
Node.val ≤ 5000head=list_node([7, -2, 0, 15])
[15, 0, -2, 7]
Run checks the first 3 tests in your browser. Submit checks all 102 tests on the server, hidden ones included.