GATE CSE 2025 Session 1 Q62: Nodes left in L1 after deleting values found in L2
- Programming and Data Structures
- 2 marks
- NAT
- No negative marking
Question
Let LIST be a datatype for an implementation of linked list defined as follows:
typedef struct list {
int data;
struct list *next;
} LIST;
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;
}
Suppose a program has created two linked lists, L1 and L2, whose contents are given in the figure (code for creating L1 and L2 is not provided here). L1 contains 9 nodes, and L2 contains 7 nodes.
The code segment in main above 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)
Try it: numerical answer: type the value
Show answer and explanation
Answer: 5 (official GATE 2025 final answer key)
Concept used: Deleting nodes through a trailing pointer: only nodes after the head are tested, and a removed node does not advance the pointer.
First step: Walk L1 from the second node and delete each node whose value is in L2 (1, 11, 6, 9, 15, 12, 4); the head is never checked.
Full step-by-step solution
Detailed working, common mistakes and linked mock tests will be available with a free GATEiit account. Log-in is coming soon.
Share this question
Source: official GATE 2025 Session 1 CS question paper and final answer key, organised by IIT Roorkee. Question paper · Answer key
More Programming and Data Structures PYQs
- 2026 Session 1 · Q23Non-leaf indices in a 1-indexed binary heap with n odd
- 2026 Session 1 · Q24Empty slots after linear-probing inserts with h(x) = (x+7) mod 11
- 2026 Session 1 · Q33Maximum height of a full binary tree with 23 nodes
- 2026 Session 1 · Q34Output of a C function with a shadowed block variable
- 2026 Session 1 · Q39Recursive length of a non-empty singly linked list
- 2026 Session 1 · Q40Third insertion that can never yield a complete BST of 1..15