Skip to main content

GATE CSE 2025Data Structures › Stacks, Queues, Linked Lists & ArraysVerified

GATE CSE 2025 PYQ: Let L I S T \mathrm{LIST} LIST be a datatype for an implementation of linked list defined as follows:…

Let LIST\mathrm{LIST} be a datatype for an implementation of linked list defined as follows:

  1. typedef struct list {
  2. int data;
  3. struct list *next;
  4. } LIST;

Suppose a program has created two linked lists, L1L1 and L2L2, whose contents are given in the figure below (code for creating L1L1 and L2L2 is not provided here). L1L1 contains 9 nodes, and L2L2 contains 7 nodes.

Consider the following C program segment that modifies the list L1L1. The number of nodes that will be there in L1L1 after the execution of the code segment is __________. (Answer in integer)

GATE CSE 2025 Stacks, Queues, Linked Lists & Arrays diagram: Let L I S T \mathrm{LIST} LIST be a datatype for an implemen

  1. int find (int query, LIST *list) {
  2. while (list != NULL) {
  3. if(list->data == query) return 1 ;
  4. list = list->next;
  5. }
  6. return 0 ;
  7. }
  8. int main (){
  9. ... ... ...
  10. ptr1=L1; ptr2=L2;
  11. while (ptr1->next != NULL){
  12. query = ptr1->next->data;
  13. if (find (query, L2))
  14. ptr1->next = ptr1->next->next;
  15. else ptr1 = ptr1->next;
  16. }
  17. ... .... ...
  18. return 0;
  19. }

    Answer

    5

    Approach

    The program walks a pointer ptr1 through L1 and, for each node that follows ptr1, deletes it if its value also occurs in L2. Because ptr1 begins at the head of L1 and the loop checks ptr1->next, the first node is never tested or removed; all other nodes matching an element of L2 get unlinked.

    The loop removes every node of L1 (except the head) whose data appears in L2. Four such nodes (12, 9, 11, 15) are unlinked from the original 9, leaving 1 → 7 → 3 → 5 → 8, i.e., 5 nodes. All community answers agree on 5.

    Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.