Remove Duplicates From an Unsorted Linked List - Problem
Given the head of a linked list, find all the values that appear more than once in the list and delete the nodes that have any of those values.
Return the linked list after the deletions.
Note: A value that appears multiple times should have all its occurrences removed from the list.
Input & Output
Example 1 — Basic Case
$
Input:
head = [1,2,3,2]
›
Output:
[1,3]
💡 Note:
Value 2 appears twice, so remove all nodes with value 2. Keep nodes with values 1 and 3 that appear only once.
Example 2 — Multiple Duplicates
$
Input:
head = [2,1,1,2]
›
Output:
[]
💡 Note:
Both values 1 and 2 appear twice, so all nodes are removed. The result is an empty list.
Example 3 — No Duplicates
$
Input:
head = [1,2,3]
›
Output:
[1,2,3]
💡 Note:
All values appear exactly once, so no nodes are removed. The original list is returned.
Constraints
- The number of nodes in the given list is in the range [1, 105]
- 1 ≤ Node.val ≤ 105
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code