← DiffPush

Delete nth node from back

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

Cancelling the nth Reservation from the Back

A booking chain lists seats in order, and a cancellation names a seat counted from the rear — awkward on a one-way chain with no length posted. The counter settles it with a two-pointer walk: send one pointer n seats ahead, then advance both until the leader runs out; the follower now stands exactly one seat before the cancellation target.

Input: An array head of node values and an integer n, the distance from the end of the list.

Output: The values of the list after removing the n-th node from the end.

Constraints

Examples

Example 1

Input: {"head":[1,2,3,4,5],"n":2}
Output: [1,2,3,5]
Counting from the rear, the second seat is 4; relinking skips it.

Example 2

Input: {"head":[1],"n":1}
Output: []
The only seat is cancelled, leaving an empty chain.

Solve this in your browser →

Also on LeetCode ↗