GATE / CS ARCHIVE

GATE CSE 1997 / QUESTION 18(c)

Interpret the duplicate counter

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

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

Arrays & Linked Lists

Question 18(c), 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 does the integer *j contain when the function completes?
Question 18(c), 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 does the integer *j contain when the function completes?
Question 18(c), 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 does the integer *j contain when the function completes?
Tap to enlarge
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.

Original paper, page 9