← DiffPush

Delete nodes from dll

Standard Bar Linked List · Medium Problems of DLL O(n) · O(1)

Purging Recalled Batches from the Two-Way Sorter

A distribution center's two-way sorter tracks bins of medication batches, and a recall notice flags one batch ID. Every bin carrying that ID must be pulled from the belt without disturbing the order of the rest — whether the bin sits at the intake end, the discharge end, or anywhere between. Because each station knows both neighbours, a pulled bin's two neighbours can re-link to each other directly.

Input: An array arr of n node values forming the doubly linked list, and an integer key.

Output: The values of the list after every node equal to key is unlinked, read from the new head.

Constraints

Examples

Example 1

Input: {"arr":[2,2,10,8,4,2,5,2],"key":2}
Output: [10,8,4,5]
Every station carrying the recalled batch 2 is pulled; the four clean stations keep their relative order.

Example 2

Input: {"arr":[4,4,6],"key":4}
Output: [6]
Removals at the intake end reassign the head itself — after both 4s are pulled, 6 is the new entry point.

Solve this in your browser →

Also on LeetCode ↗