#K76482. Remove Duplicates from a Linked List
Remove Duplicates from a Linked List
Remove Duplicates from a Linked List
You are given a singly linked list of integers. Your task is to remove all the nodes that have duplicate numbers, leaving only the distinct numbers from the original list. In other words, if a value appears more than once, completely remove it from the list. The order of the remaining nodes should be the same as in the original list.
Input Format: The first line contains an integer n which denotes the number of nodes. The second line contains n space-separated integers representing the values of the nodes in order.
Output Format: Output the values of the linked list after removing duplicates. The remaining values should be printed in order separated by a single space. If no nodes remain, print an empty line.
Example:
If the input is:
5 1 2 3 2 1
Then the output should be:
3
The challenge requires careful handling of list nodes and frequency counting. Use appropriate data structures to ensure that you achieve the correct result.
inputFormat
The input is given via standard input (stdin). The first line contains an integer n denoting the number of nodes in the linked list. The second line contains n space-separated integers representing the linked list nodes.
If n is 0, then the linked list is empty.
outputFormat
Output the resulting linked list to standard output (stdout) with the unique node values separated by a single space. If the resulting list is empty, output an empty line.
## sample5
1 2 3 2 1
3
</p>