class RemoveListDuplicatedElements {

  public void remove(ListNode<Integer> head) {
    validateInput(head);

    Set<Integer> elements = new HashSet<Integer>();
    ListNode<Integer> currentNode = head;
    ListNode<Integer> prevNode = null;
    while (currentNode != null) {
      if (elements.contains(currentNode.getData())) {
        prevNode.setNext(currentNode.getNext());
      } else {
        elements.add(currentNode.getData());
      }
      prevNode = currentNode;
      currentNode = currentNode.getNext();
    }
  }

}