ITPEC FE Subject B October 2025 Question 7

From the answer group below, select the correct combination of answers to be inserted into A and B in the program.

A singly linked list can be reversed using the following recursive procedure.

Let head be the first element of the list, and let elm be undefined.
(a) Reverse the order of the elements in the list starting from head, and set the next element of the last element in the reversed list—that is, the next element of head—to elm.

To “reverse the order of the elements in the list starting from head,” reverse the list starting from the next element of head, and then connect head to the end of the reversed list. In other words, execute (a) by setting head as elm, and the next element of head as head.

The recursive function reverseList reverses a singly linked list and returns the head of the reversed list. Initially, the function reverseList is called as reverseList(head, undefined). Here, head is the head of the singly linked list before reversal and is of type ListElement. The table provides an explanation of the member variables of the class ListElement. ListElement-type variables store references to instances of the class ListElement.

Table Explanation of the member variables of the class ListElement

Member variableTypeDescription
valintegerThe value of the element.
nextListElementReference to the next element, if there is no next element, the status is undefined.
[Program]
○ ListElement: reverseList(ListElement: head, ListElement: elm)
ListElement: listHead
if (head.next is not undefined)
listHead ← reverseList(A, head)
else
listHead ← head
endif
head.next ← B
return listHead

Answer group

OptionAB

Explanation

A: head.next, B: elm — recurse on the rest of the list with the current element passed as its successor-to-be, then point head.next at elm.

  • •The description says to execute (a) with head as elm and the next element of head as head. The call already passes head as the second argument, so A is head.next.
  • •After the rest of the list is reversed, head is its last element, and (a) sets the next element of head to elm: B is elm.

Trace for 1 → 2 → 3, starting with reverseList(1, undefined):

  • •the call for 1 calls reverseList(2, 1), which calls reverseList(3, 2)
  • •3 has no next, so listHead is 3; the next of 3 becomes 2; 3 is returned
  • •back in the call for 2: the next of 2 becomes 1; 3 is returned
  • •back in the call for 1: the next of 1 becomes undefined; 3 is returned

The list is now 3 → 2 → 1, and 1 is correctly the last element.

Answer (a)

Why not others:

  • •(b) — B elm.next: in the first call elm is undefined, so elm.next cannot be read; in the deepest call it sets the next of 3 to the old next of 2, which is 3 itself, a self-loop
  • •(c), (d) and (e) — A elm.next: in the first call elm is undefined, and in any later call elm.next is head itself, so the recursion never moves forward

Key rule: A recursive list algorithm must recurse on next, and the link it rewrites after the call uses the value the caller passed down.

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.