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

Member variableTypeDescription
valintegerThe value of an element.
nextListElementReference to the instance that holds the next element in the list. If no next element exists, the status is undefined.
[Program]
○ ListElement: mergeTwoSortedList(ListElement: head1,
ListElement: head2)
ListElement: result
if (head1 is undefined)
return head2
elseif (head2 is undefined)
return head1
endif
if (A)
result ← head1
result.next ← mergeTwoSortedList(B, head2)
else
result ← head2
result.next ← mergeTwoSortedList(head1, C)
endif
return result

Answer group

OptionABC

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.val is less than or equal to head2.val, head1 becomes the result node
  • •B: head1 is used up, so the call continues with head1.next and the whole of head2
  • •C: the mirror case; head2 was taken, so the call passes head1 and head2.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 head1 is the last element of its list, which says nothing about order, so the result is not sorted
  • •(e) and (f) — take head1 only on equal values and send every unequal pair to the head2 branch, even when head1 is 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.