GATE CS 2025 Set 1 | Question 62

Programming & Data Structures › Arrays & Linked Lists · NAT · 2 marks · Answer: 5

  1. Question 62Programming & Data Structuresnat2 marks
    Two linked lists are given (contents shown). L1 has 9 nodes, L2 has 7 nodes:
    L1: 1 -> 7 -> 12 -> 3 -> 9 -> 5 -> 11 -> 15 -> 8 -> NULL
    L2: 1 -> 11 -> 6 -> 9 -> 15 -> 12 -> 4 -> NULL
    The code below modifies L1. The number of nodes in L1 after execution 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;   // unlink
            else ptr1 = ptr1->next;
        }
        return 0;
    }

    Answer: 5

    Solution

    Answer: 5 The loop walks L1 and deletes every node (after the head) whose value also appears in L2. The head node (value 1) is never examined for deletion (the loop looks at ptr1->next). L2 as a set . Checking L1's non-head nodes:
    7  -> keep     12 -> remove   3  -> keep
    9  -> remove   5  -> keep     11 -> remove
    15 -> remove   8  -> keep
    Removed: → 4 nodes deleted. Remaining L1 .

More GATE PYQ on Arrays & Linked Lists

All GATE CS Programming & Data Structures PYQ →

Attempt GATE CS 2025 Set 1 in real GATE exam mode

Full paper with a timer, the GATE interface and a topic-wise analysis — free.

Create free account →