Arrays & Linked Lists



Answer & solution
Answer: Minimum 0; maximum n−1. For d distinct values, the block runs d−1 times. (no official key was published)
The list is sorted, so equal values occur in contiguous runs. The first run's first node is retained before the loop begins. Thereafter S2 executes precisely when t2 reaches the first node of a new value run, because its value differs from that in the last retained node t1.
If the input has d distinct values, there are d runs and exactly d−1 transitions to new runs. Thus the S2 block, and each of its three statements, executes d−1 times. Since 1≤d≤n for a nonempty list, the minimum is 0 and the maximum is n−1.
The minimum occurs when all n values are equal: every comparison S1 is false, t1 stays at the first node, and the final t1->next=NULL detaches all duplicate nodes from the returned chain. The maximum occurs when all n values are distinct: every one of the n−1 loop iterations takes S2, retains the current node and advances t1.
For [1,1,2,2,3], d=3 and S2 runs twice, once on the first 2 and once on the first 3. It does not run once per duplicate removed. This distinction prevents reversing the extremes: an all-equal list removes the most duplicate links but executes S2 the fewest times. The final NULL assignment is outside S2 and is not counted as one of these block executions.