Reverse a Linked List
easyA document editor stores its undo history as a singly linked chain, oldest action first. To replay history newest-first, you need the whole chain flipped — in place, no second copy (the chain can be huge).
Given the head of a singly linked list, reverse it and return the new head. Tests pass the list as a plain array; build and serialize handle the conversion — your work happens in _reverse, on real nodes.
Example 1
in values = [1, 2, 3, 4, 5]
out [5, 4, 3, 2, 1]
Example 2
in values = [1, 2]
out [2, 1]
Example 3
in values = []
out []
An empty list reverses to an empty list.
Constraints
- · 0 <= number of nodes <= 5000
- · -5000 <= node value <= 5000
- · O(1) extra space — reverse in place
solution.py● loading python…
test results
Hit ▶ Run to test your code against the visible cases.