GATE / CS ARCHIVE

GATE CSE 1997 / QUESTION 18(b)

Bound duplicate-removal updates

Data Structures · Arrays & Linked Lists · Full GATE CSE 1997 paper

GATE 1997, Q18(b) 2 marks · Descriptive
Report issue

Arrays & Linked Lists

Question 18(b), GATE 1997: The following C-like code removes duplicate values from a sorted singly linked list. Node has int val and Node *next. Assume the input is acyclic and contains n≥2 nodes; j points to a separate valid integer. Pointer-member syntax is normalized from the reproduced typesetting.
Node *remove_duplicates(Node *head,int *j) {
  Node *t1,*t2;
  *j=0;
  t1=head;
  if(t1!=NULL) t2=t1->next;
  else return head;
  *j=1;
  if(t2==NULL) return head;
  while(t2!=NULL) {
    if(t1->val!=t2->val) {             /* S1 */
      (*j)++; t1->next=t2; t1=t2;      /* S2 */
    }
    t2=t2->next;
  }
  t1->next=NULL;
  return head;
}
What are the minimum and maximum numbers of executions of the block S2?
Question 18(b), GATE 1997: The following C-like code removes duplicate values from a sorted singly linked list. Node has int val and Node *next. Assume the input is acyclic and contains n≥2 nodes; j points to a separate valid integer. Pointer-member syntax is normalized from the reproduced typesetting.
Node *remove_duplicates(Node *head,int *j) {
  Node *t1,*t2;
  *j=0;
  t1=head;
  if(t1!=NULL) t2=t1->next;
  else return head;
  *j=1;
  if(t2==NULL) return head;
  while(t2!=NULL) {
    if(t1->val!=t2->val) {             /* S1 */
      (*j)++; t1->next=t2; t1=t2;      /* S2 */
    }
    t2=t2->next;
  }
  t1->next=NULL;
  return head;
}
What are the minimum and maximum numbers of executions of the block S2?
Question 18(b), GATE 1997: The following C-like code removes duplicate values from a sorted singly linked list. Node has int val and Node *next. Assume the input is acyclic and contains n≥2 nodes; j points to a separate valid integer. Pointer-member syntax is normalized from the reproduced typesetting.
Node *remove_duplicates(Node *head,int *j) {
  Node *t1,*t2;
  *j=0;
  t1=head;
  if(t1!=NULL) t2=t1->next;
  else return head;
  *j=1;
  if(t2==NULL) return head;
  while(t2!=NULL) {
    if(t1->val!=t2->val) {             /* S1 */
      (*j)++; t1->next=t2; t1=t2;      /* S2 */
    }
    t2=t2->next;
  }
  t1->next=NULL;
  return head;
}
What are the minimum and maximum numbers of executions of the block S2?
Tap to enlarge
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.

Original paper, page 9