Katkat

Remove duplicates from a singly linked list

SalesforceSoftware Engineer · HR Screen

Last 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, or null at the tail)
  • The relative order of the remaining nodes must stay the same.

Return the head of the updated linked list.

Examples

  1. Input: 3 -> 4 -> 3 -> 6 Output: 3 -> 4 -> 6

  2. Input: 3 -> 4 -> 3 -> 2 -> 6 -> 1 -> 2 -> 6 Output: 3 -> 4 -> 2 -> 6 -> 1