• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

GATE | GATE CS 1997 | Question 62

Consider the following piece of \'C\' code fragment that removes duplicates from an ordered list of integers.

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;
}

Assume the list contains n elements (n≥2) in the following questions. a). How many times is the comparison in statement S1 made? b). What is the minimum and the maximum number of times statements marked S2 get executed? c). What is the significance of the value in the integer pointed to by j when the function completes?

(A)

  • (a). n-1 times, since comparison is pairwise for n elements.
  • (b). maximum : n-1 for all distinct elements, minimum: 0 for all same elements.
  • (C). j keeps count of distinct nodes in the list.

(B)

  • (a). n times, since comparison is pairwise for n elements.
  • (b). maximum : n-1 for all distinct elements, minimum: 0 for all same elements.
  • (C). j keeps count of distinct nodes in the list.

(C)

  • (a). n-1 times, since comparison is pairwise for n elements.
  • (b). maximum : n-1 for all distinct elements, minimum: 1 for all same elements.
  • (C). j keeps count of distinct nodes in the list.

(D)

None of the above

Answer

Please comment below if you find anything wrong in the above post
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Last Updated :
Share your thoughts in the comments