GATE / CS ARCHIVE

GATE CSE 2025 SET 1 / QUESTION 62

Tracing successor deletion in a linked list

Data Structures · Arrays & Linked Lists · Full GATE CSE 2025 Set 1 paper

GATE 2025 Set 1, Q62 2 marks · NAT
Report issue

Arrays & Linked Lists

Question 62, GATE 2025: L1 is 1→7→12→3→9→5→11→15→8→NULL and L2 is 1→11→6→9→15→12→4→NULL. find(q,L2) returns whether q occurs in L2. Starting ptr1=L1, repeat while ptr1->next!=NULL: set query=ptr1->next->data; if find(query,L2), execute ptr1->next=ptr1->next->next; otherwise advance ptr1=ptr1->next. How many nodes remain reachable in L1?
Tap to enlarge
Answer & solution

Official answer: 5

Approach: Notice that the tested node is ptr1’s successor, not ptr1 itself. On deletion the pointer stays still, so the replacement successor is tested next.
Initially ptr1 points to the head value 1. Its successor 7 is not in L2, so advance to 7. The next value 12 is in L2, so bypass it, leaving 7 pointing to 3. Keep ptr1 at 7, test 3, and then advance to 3 because it is absent from L2.
Delete successor 9, retain and advance to 5, then delete 11 and 15 in consecutive iterations while ptr1 remains at 5. Finally retain 8 and stop because its next pointer is NULL.
The resulting list is 1→7→3→5→8, so five nodes remain. Although head value 1 also occurs in L2, it is never tested as a successor and is not removed. Advancing ptr1 after a deletion would incorrectly skip a candidate such as 15. The code unlinks nodes without calling free; the question asks reachability in L1, not the count of still-allocated memory objects.

Original paper, page 62 · Answer key