← DiffPush

Deleting node in linked list

Baseline Linked List · Single Linked List O(N) · O(1)

Pulling One Car Off the Train

A parcel train's cars are chained one-way, and a manifest update demands that the x-th car (counted from 1) be uncoupled. Crews can only relink couplings — the car before the target must connect straight to the car after it, and the lead car is special because nothing precedes it. Report the train once the car is gone.

Input: An array arr of node values, and an integer x, the 1-based position of the node to delete.

Output: The list after removing the x-th node, read front to back.

Constraints

Examples

Example 1

Input: {"arr":[1,3,4],"x":3}
Output: [1,3]
The third car is the tail; uncoupling it just leaves the first two cars.

Example 2

Input: {"arr":[1,5,2,9],"x":2}
Output: [1,2,9]
Removing the second car relinks 1 directly to 2, skipping 5.

Solve this in your browser →

Also on LeetCode ↗