← DiffPush

Reverse DLL

Baseline Linked List · Doubly Linked List O(n) · O(1)

Running the Assembly Line the Other Way

A plant repurposes a conveyor whose stations are two-way linked, and the retrofit requires the line to run in the opposite direction. Each station's forward and backward rails swap roles, and the station that used to hand work off into nowhere becomes the new entry point. Report the station order from the new entry.

Input: An array arr of n node values representing the doubly linked list.

Output: The list after reversal, read from the new head.

Constraints

Examples

Example 1

Input: {"arr":[3,4,5]}
Output: [5,4,3]
The line's exit station (5) becomes its entry.

Example 2

Input: {"arr":[75,122,59,196]}
Output: [196,59,122,75]
Four stations, every rail pair swapped, new order starts at 196.

Solve this in your browser →

Also on LeetCode ↗