← DiffPush

Reverse LL

Standard Bar Linked List · Medium Problems of LL O(N) · O(1)

Sending the Relay Back Down the Line

A one-way relay line must suddenly run in reverse — every worker's outbound link is re-aimed at the person who used to point at them, and the last worker becomes the new starter. Each rewiring happens one worker at a time while walking the line, and nobody may lose track of the worker ahead while their own link is being flipped.

Input: An array head of node values representing the linked list.

Output: The values of the reversed list, read from the new head.

Constraints

Examples

Example 1

Input: {"head":[1,2,3,4,5]}
Output: [5,4,3,2,1]
Every link flips and the tail node 5 opens the reversed chain.

Example 2

Input: {"head":[1,2]}
Output: [2,1]
A two-node chain needs exactly one link flip.

Solve this in your browser →

Also on LeetCode ↗