Arrays & Linked Lists



Answer & solution
Answer: The number of distinct input values, equal to the number of nodes in the returned deduplicated list. (no official key was published)
For a nonempty list, the code explicitly sets *j=1 before entering the loop. This counts the first retained value. Every later S2 execution marks the first encounter with a new value and increments *j once. Since sorted order puts equal values together, no later run repeats an earlier value.
If there are d distinct input values, S2 executes d−1 times and the final value is 1+(d−1)=d. The returned chain contains exactly one representative of each run, so d is also its number of nodes. For [2,2,5,5,5,9], the initialization counts 2, and the two increments count 5 and 9, giving *j=3. The number of removed duplicate nodes is n−*j, not *j itself.
The early cases are consistent even though this question assumes n≥2. An empty list returns with *j=0; a one-node list returns with *j=1. The *j=1 assignment after the empty-list check is therefore essential to interpreting the result. Omitting it would incorrectly leave the counter at d−1.
The function changes links but does not free detached nodes. Its logical output is deduplicated, but claiming that it releases their memory would go beyond the code. Sortedness is also essential for “distinct values”: on an unsorted list such as [1,2,1], the code would count three runs rather than two globally distinct values. This explanation applies to the ordered-list precondition actually given.