← DiffPush

Introduction to Double LL

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

Walking the Corridor Backwards

A maintenance robot patrols a two-way corridor where every checkpoint knows both its neighbours. The evening routine sends it out the far end and requires the checkpoint sequence to be relabelled from that direction: each checkpoint's two link registers swap roles, and the last checkpoint of the original corridor becomes the new entrance. Produce the corridor as seen from its new entrance.

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]
Three checkpoints relabelled from the far end read 5, 4, 3.

Example 2

Input: {"arr":[75,122,59,196]}
Output: [196,59,122,75]
The far checkpoint (196) becomes the new entrance.

Solve this in your browser →

Also on LeetCode ↗