GATE / CS ARCHIVE

GATE CSE 1997 / QUESTION 18(a)

Count duplicate-removal comparisons

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

GATE 1997, Q18(a) 1 mark · Descriptive
Report issue

Arrays & Linked Lists

Question 18(a), 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;
}
How many times is the comparison S1 evaluated?
Question 18(a), 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;
}
How many times is the comparison S1 evaluated?
Question 18(a), 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;
}
How many times is the comparison S1 evaluated?
Tap to enlarge
Answer & solution

Answer: n−1. (no official key was published)

The first input node is taken as the initial retained node t1. Before the loop, t2 is set to the second input node. The loop then advances t2 exactly once through every remaining original node, stopping at NULL. Since there are n−1 nodes after the first, S1 is evaluated exactly n−1 times.

Rewiring t1->next inside S2 does not skip any as-yet unvisited node. When S2 executes, t1 is moved to the current t2, but that current node's own next pointer has not been changed. The subsequent t2=t2->next therefore advances to its original successor. When S2 does not execute, t1 remains the retained node and the same successor step still happens. Thus all-equal and all-distinct lists perform the same number of value comparisons.

For example, on [1,1,2,2], S1 compares the second 1, first 2, and second 2 against the current retained value, for three comparisons. The while-condition test is a different comparison and is not included in this count.

This leaf uses the full shared stem and belongs to that single group; it is not evidence for an extra exam question. The displayed code restores legal pointer-member notation while retaining its algorithmic operations.

Original paper, page 9