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)

Correct : 5
The number of nodes remaining in L1 after execution is 5.
The code does one thing — it walks through L1 and deletes any node whose data value is also present in L2. The find() function performs a linear search through L2 for each node of L1.
The logic works like this: ptr1 always points to the node just before the one being checked. If ptr1->next->data is found in L2, the next node gets deleted by setting ptr1->next = ptr1->next->next, bypassing it entirely. If not found, ptr1 simply advances to the next node.
Note that ptr1 never moves when a deletion happens — this ensures consecutive matching nodes are all caught and removed without skipping any.
From the given lists, 4 out of 9 nodes in L1 have data values that also appear in L2. After those 4 deletions, 9 - 4 = 5 nodes remain in L1.
Similar Questions
Total Unique Visitors