Remove Duplicates from Sorted List
Practice this coding question on Coding Gurukul.
Problem statement
A laboratory stores sorted sample identifiers in a singly linked chain. Repeated identifiers refer to the same sample and must be removed while retaining the chain order. The laboratory wants the remaining chain to contain exactly one node for every distinct identifier. Your task Build the linked list from the supplied values, then remove duplicate nodes from this sorted list. Print the values of the resulting list in traversal order. Important details Keep one node from each group of equal values; do not delete the entire group. Rewire next pointers using O(1) auxiliary space. An empty chain remains empty, and an already unique chain remains unchanged. Worked example The chain 2 -> 2 -> 5 -> 5 -> 5 -> 9 becomes 2 -> 5 -> 9. Each identifier remains once, so the output is 2 5 9.
Input format
The first line contains n. The next line contains n space-separated integers; for n = 0 this line may be empty.
Output format
Print the resulting values in order, separated by spaces. Print EMPTY if there are no values.
Constraints
0 <= n <= 300; -100 <= values <= 100; values are nondecreasing.
Sample input
6 2 2 5 5 5 9
Sample output
2 5 9
Sign in to run or submit a solution in C++, Java or Python.