Remove duplicates from a singly linked list
SalesforceSoftware Engineer · HR ScreenLast updated
This question evaluates understanding of singly linked list data structures and in-place node manipulation to remove repeated values while preserving relative order, testing competencies in pointer management and duplicate-detection.
You are given the head of a singly linked list of integers. Modify the list in place so that it contains only the first occurrence of each value (i.e., remove any node whose value has already appeared earlier in the list).
- Each node has fields:
data(integer)next(reference to next node, ornullat the tail)- The relative order of the remaining nodes must stay the same.
Return the head of the updated linked list.
Examples
-
Input:
3 -> 4 -> 3 -> 6Output:3 -> 4 -> 6 -
Input:
3 -> 4 -> 3 -> 2 -> 6 -> 1 -> 2 -> 6Output:3 -> 4 -> 2 -> 6 -> 1
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.