← Salesforce Interview Insights
This was way more layered than I expected for what sounded like a warmup question.
Start by defining the ListNode type and clarifying the sorted vs unsorted cases. For sorted lists, use a single pointer to skip duplicates in O(n) time and O(1) space. For unsorted lists, present two solutions: one using a hash set for O(n) time and O(n) space, and one using the runner technique for O(n^2) time and O(1) space, explaining trade-offs.
Pro tip: Emphasize that the runner technique, while O(n^2), is valuable when memory is constrained, and always discuss edge cases and test them to demonstrate thoroughness.
Define the ListNode class with val and next fields. Clearly distinguish between sorted and unsorted list scenarios and the constraints for each.
Traverse the list with a current pointer. If current.next has the same value, skip it by adjusting pointers; otherwise, move to the next node. This removes duplicates in-place with O(1) extra space.
Traverse the list, keeping a hash set of seen values. If a value is already in the set, remove the node by adjusting pointers; otherwise, add it to the set and move forward. This preserves first occurrences.
For each node, use a runner pointer to scan ahead and remove any subsequent nodes with the same value. This uses O(1) extra space but O(n^2) time.
For each approach, state time and space complexity. Write tests covering empty list, single node, all duplicates, and no duplicates to verify correctness.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.