ITPEC FE Subject B April 2026 Question 10
From the answer group below, select the correct combination of answers to be inserted into A through C in the program.
The function mergeTwoSortedList concatenates two linear sorted linked lists in a sorted order and returns one linear linked list of sorted order. Each element of the linear linked lists is represented by the class ListElement. The table lists the description of the class ListElement. ListElement-type variable holds a reference to an instance of the class ListElement. Here, head1 and head2 represent the headers of the two sorted linked lists, respectively and are of type ListElement. After concatenation of two existing linear linked lists, the function returns a linear linked list headed with result, which is of type ListElement. Both the input linked lists and returned linked list are sorted in ascending order of the member variable val.
Table Explanation of the member variables of the class ListElement
Answer group
Explanation
A: head1.val ≤ head2.val, B: head1.next, C: head2.next — the merge takes the smaller of the two front elements and recurses on what is left.
- •A: when
head1.is less than or equal toval head2.,val head1becomes the result node - •B:
head1is used up, so the call continues withhead1.and the whole ofnext head2 - •C: the mirror case;
head2was taken, so the call passeshead1andhead2.next
Trace for 1 → 4 and 2 → 3: take 1, then 2 (since 4 > 2), then 3; now head2 is undefined, so the rest of the first list, 4, is returned as is. The result is 1, 2, 3, 4.
Answer (c)
Why not others:
- •(a) and (b) — test whether
head1is the last element of its list, which says nothing about order, so the result is not sorted - •(e) and (f) — take
head1only on equal values and send every unequal pair to thehead2branch, even whenhead1is smaller - •(g) and (h) — use ≥ and take the larger head first; for 1 → 5 and 2 → 3 that gives 2, 3, 1, 5, which is not sorted in either direction
- •(b), (d), (f) and (h) — advance the other list in the recursive call, so the rest of the list just taken from is lost and the other list's elements are passed twice
Key rule: In a recursive merge exactly one list advances per call, and it is the one whose element was just placed in the result.
Linked List · AI-written, may contain errors
This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.