Remove duplicates in linked list
Company: Salesforce
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
Overview: This question evaluates proficiency in linked list manipulation, duplicate detection and removal, and algorithmic complexity analysis for both sorted and unsorted data.
Constraints
- 0 <= len(values) <= 200000
- Values fit in 32-bit signed integer range
- If is_sorted is True, values is non-decreasing
- Preserve the relative order of first occurrences in the output
- Expected time: O(n); Space: O(1) if is_sorted else O(n)
Hints
- If the list is sorted, compare each value to the last kept value to skip duplicates.
- If the list is unsorted, use a hash set to track seen values and keep only the first occurrence.
- Ensure the output preserves the order of first occurrences.