GateAI
Programming in C

GATE CSE 2025 Set 1 · Q62

NAT2 marksMedium

Time

Let LIST be a datatype for an implementation of linked list defined as follows:typedef struct list { int data; struct list *next; } LIST;Suppose a program has created two linked lists, L1 and L2, whose contents are given in the figure below (code for creating L1 and L2 is not provided here). L1 contains 9 nodes, and L2 contains 7 nodes. Consider the following C program segment that modifies the list L1. The number of nodes that will be there in L1 after the execution of the code segment is _________ . (Answer in integer)int find (int query, LIST *list) { while (list != NULL) { if(list->data == query) return 1; list = list->next; } return 0; } int main () { ... ... ... ptr1=L1; ptr2=L2; while (ptr1->next != NULL) { query = ptr1->next->data; if (find (query, L2)) ptr1->next = ptr1->next->next; else ptr1 = ptr1->next; } ... ... ... return 0; }

GATE CSE 2025 Set 1 Q62 diagram

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 5

Programming in C
  1. Initial List States:

    • L1=[1→7→12→3→9→5→11→15→8]\text{L1} = [1 \to 7 \to 12 \to 3 \to 9 \to 5 \to 11 \to 15 \to 8]

    • L2=[1→11→6→9→15→12→4]\text{L2} = [1 \to 11 \to 6 \to 9 \to 15 \to 12 \to 4]

    • Elements present in L2\text{L2} are {1,4,6,9,11,12,15}\{1, 4, 6, 9, 11, 12, 15\}.

  2. Functionality of the Code:

    • The function find(query, L2) returns 1 if query is in L2\text{L2}, and 0 otherwise.

    • The while (ptr1->next != NULL) loop in main() checks every node in L1\text{L1} following ptr1 (i.e., starting from the 2nd node of L1\text{L1}):

      • If the next node's value is in L2\text{L2}, it bypasses/deletes it from L1\text{L1} via ptr1->next = ptr1->next->next without advancing ptr1.

      • If it is not in L2\text{L2}, it advances ptr1 by ptr1 = ptr1->next.

  3. Step-by-step Trace:

    • Initially, ptr1 points to node with value 11.

    • Step 1:query = 77 (next node). 7∉L2  ⟹  7 \notin \text{L2} \implies ptr1 advances to 77.

    • Step 2:query = 1212. 12∈L2  ⟹  12 \in \text{L2} \implies node 1212 is removed. ptr1 still points to 77, and its next becomes 33.

    • Step 3:query = 33. 3∉L2  ⟹  3 \notin \text{L2} \implies ptr1 advances to 33.

    • Step 4:query = 99. 9∈L2  ⟹  9 \in \text{L2} \implies node 99 is removed. ptr1 still points to 33, and its next becomes 55.

    • Step 5:query = 55. 5∉L2  ⟹  5 \notin \text{L2} \implies ptr1 advances to 55.

    • Step 6:query = 1111. 11∈L2  ⟹  11 \in \text{L2} \implies node 1111 is removed. ptr1 still points to 55, and its next becomes 1515.

    • Step 7:query = 1515. 15∈L2  ⟹  15 \in \text{L2} \implies node 1515 is removed. ptr1 still points to 55, and its next becomes 88.

    • Step 8:query = 88. 8∉L2  ⟹  8 \notin \text{L2} \implies ptr1 advances to 88.

    • Step 9:ptr1->next is NULL, loop terminates.

  4. Final List L1\text{L1}:

    L1=[1→7→3→5→8]\text{L1} = [1 \to 7 \to 3 \to 5 \to 8]

    The remaining number of nodes in L1\text{L1} is 5.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Programming in C previous year question is Q62 of the GATE CSE 2025 Set 1 paper. Find more GATE CSE PYQs on Programming in C in Programming and Data Structures, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Programming in C PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Programming in C in GATE CSE

Programming in C has 74 GATE CSE PYQs across 25 papers from 2011 to 2026. 17 of them are 2-mark NAT questions like this one, and GATE CSE 2025 Set 1 has 3.

How GATE tests Programming in C

More from GATE CSE 2025 Set 1

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2025 Set 1 · Q62 · NAT

Let LIST be a datatype for an implementation of linked list defined as follows: typedef struct list { int data; struct list *next; } LIST; Suppose a program has created two linked lists, L1 and L2, whose contents are given in the figure below (code for creating L1 and L2 is not provided here). L1 contains 9 nodes, and L2 contains 7 nodes. Consider the following C program segment that modifies the list L1. The number of nodes that will be there in L1 after the execution of the code segment is … (Answer in integer) int find (int query, LIST *list) { while (list != NULL) { if(list->data == query) return 1; list = list->next; } return 0; } int main () { ... ... ... ptr1=L1; ptr2=L2; while (ptr1->next != NULL) { query = ptr1->next->data; if (find (query, L2)) ptr1->next = ptr1->next->next; else ptr1 = ptr1->next; } ... ... ... return 0; }

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.