In a singly linked list, which approach finds the i-th node from the end in a single pass?

Master Linked Lists Structures for Data Structures Tests. Utilize flashcards and multiple choice questions with detailed explanations for each, ensuring your readiness for the exam!

Multiple Choice

In a singly linked list, which approach finds the i-th node from the end in a single pass?

Explanation:
The idea being tested is using two pointers with a fixed gap to locate a position from the end in one pass. Keep two pointers in the list with a distance of i between them. Start by moving the leading pointer i steps ahead. Then move both pointers forward together, one node at a time, until the leading pointer reaches the end (NULL). At that moment, the trailing pointer sits i nodes from the end, which is the i-th node from the end. This works in one pass because after establishing the gap, only a single synchronized walk remains to reach the end, and no extra full traversal is needed. For example, in a 1→2→3→4→5 list with i = 2, move the lead to 3, then move both pointers: lead goes to 4 and 5, then NULL, while trail follows to 4, which is the 2nd node from the end. Compared to approaches that use a stack (which adds extra memory) or that count the length and traverse twice (which takes two passes), this two-pointer gap method is space-efficient and performs the task in a single traversal. A slightly different wording of the same approach (advancing one pointer by i and then moving both until the lead hits NULL) describes the same idea in a variant form, but the standard description emphasizes maintaining the fixed gap between two pointers.

The idea being tested is using two pointers with a fixed gap to locate a position from the end in one pass. Keep two pointers in the list with a distance of i between them. Start by moving the leading pointer i steps ahead. Then move both pointers forward together, one node at a time, until the leading pointer reaches the end (NULL). At that moment, the trailing pointer sits i nodes from the end, which is the i-th node from the end.

This works in one pass because after establishing the gap, only a single synchronized walk remains to reach the end, and no extra full traversal is needed. For example, in a 1→2→3→4→5 list with i = 2, move the lead to 3, then move both pointers: lead goes to 4 and 5, then NULL, while trail follows to 4, which is the 2nd node from the end.

Compared to approaches that use a stack (which adds extra memory) or that count the length and traverse twice (which takes two passes), this two-pointer gap method is space-efficient and performs the task in a single traversal. A slightly different wording of the same approach (advancing one pointer by i and then moving both until the lead hits NULL) describes the same idea in a variant form, but the standard description emphasizes maintaining the fixed gap between two pointers.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy