Reverse a singly linked list so its nodes appear in the opposite order. Change the nodes' next links and return the values encountered from the new head.
The callable receives and returns a value-list serialization for portability. Inside the implementation, build the corresponding singly linked nodes and reverse their links. Replacing the task with array slicing or reversed-value copying does not satisfy the pointer-manipulation requirement.
Function Signature
def reverse_linked_values(values: list[int]) -> list[int]:
Rules
-
The input list represents nodes from head to tail and contains no cycle.
-
Preserve every node value, including duplicates.
-
The returned list serializes the nodes from the new head to the new tail.
-
Empty input returns an empty list.
-
Serialization is an exercise adapter around the linked-list operation.
Constraints
These are exercise bounds.
-
0 <= len(values) <= 100000
.
-
-1000000000 <= values[i] <= 1000000000
.
Examples
Input: values = [1, 2, 3, 4]
Output: [4, 3, 2, 1]
The old tail becomes the head and the old head's next pointer becomes null.
Input: values = [5, 5, 2]
Output: [2, 5, 5]
Equal values remain separate nodes and are preserved.