← DiffPush

Remove duplicates from dll

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

Squeezing Repeat SKUs out of a Sorted Rail

An inventory rail is value-sorted, but a scanner glitch booked the same SKU several times in a row. Operations wants exactly one entry per distinct SKU, first occurrence kept, and the two-way links re-fused so the rail stays coherent. Because duplicates can only be adjacent in a sorted run, a single forward pass that remembers the value of the last kept entry is enough to squeeze out every repeat.

Input: An array head of n node values forming a doubly linked list sorted in non-decreasing order.

Output: The values of the list after all duplicate nodes are unlinked, read from the head.

Constraints

Examples

Example 1

Input: {"head":[1,1,1,2,3,4]}
Output: [1,2,3,4]
The run of three 1s collapses to its first occurrence; the sorted tail is untouched.

Example 2

Input: {"head":[1,2,2,3,3,4,4]}
Output: [1,2,3,4]
Three separate duplicate runs each squeeze down to a single entry.

Solve this in your browser →

Also on LeetCode ↗