Delete Every Value That Repeats in an Unsorted Singly Linked List
SalesforceSoftware Engineer · OtherLast updated
Linked list problem: delete every node whose value occurs more than once in an unsorted singly linked list, removing all copies and keeping the remaining nodes in their original order. It tests frequency counting, list traversal and careful handling of removals, including at the head of the list.
A singly linked list holds integer values in no particular order. Delete every node whose value occurs more than once anywhere in the list, removing all copies of that value rather than only the extra ones. Keep the remaining nodes in their original relative order and return the resulting list.
For the console, the list is passed as values, its node values from head to tail, and you return the surviving node values from head to tail in the same form. Return an empty list if no node survives.
Function Signature
def remove_repeated_values(values: List[int]) -> List[int]:
Rules
- A value that occurs exactly once is kept; a value that occurs two or more times is removed entirely.
- The relative order of the surviving nodes does not change.
Constraints
1 <= len(values) <= 10^51 <= values[i] <= 10^5
Examples
Example 1
Input: values = [4, 7, 4, 9, 1, 7]
Output: [9, 1]
Values 4 and 7 each occur twice, so all four of those nodes are removed.
Example 2
Input: values = [8, 8, 8]
Output: []
Example 3
Input: values = [5]
Output: [5]
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.