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(. 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
Answer group
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. at elm.
- •The description says to execute (a) with
headaselmand the next element ofheadashead. The call already passesheadas the second argument, so A ishead..next - •After the rest of the list is reversed,
headis its last element, and (a) sets the next element ofheadtoelm: B iselm.
Trace for 1 → 2 → 3, starting with reverseList(:
- •the call for 1 calls
reverseList(, which calls2, 1) reverseList(3, 2) - •3 has no next, so
listHeadis 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.: in the first callnext elmis undefined, soelm.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-loopnext - •(c), (d) and (e) — A
elm.: in the first callnext elmis undefined, and in any later callelm.isnext headitself, 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.