Practice

Singly Linked List Traversal

2025/May/June·Variant 2
MEDIUMLinked Lists

A singly linked list is represented using two parallel arrays: ListData holds the value stored at each index, and NextPointer holds the index of the next node (or -1 for the end of the list). StartPointer holds the index of the first node.

Nodes are not necessarily stored in list order — you must follow the pointers, not the array index order. Write procedure PrintList() that outputs each value from StartPointer to the end of the list.

Input: Number of nodes, then for each storage index: its value and its next-pointer, then StartPointer. Output: Each value in list order, one per line.

Example:

Input:  3
        Bob
        2
        Amy
        -1
        Cy
        1
        0
Output: Bob
        Cy
        Amy
Premium is coming soon. All grading features are currently unlocked.

Sample Test Cases

Test 1: Nodes stored out of list order
Inputs: 3, Bob, 2, Amy, -1, Cy, 1, 0
Expected: Bob Cy Amy
Test 2: Single-node list
Inputs: 1, Solo, -1, 0
Expected: Solo