A gate scanner records boarding-pass IDs as a singly linked chain: the first node is the earliest scan, and each node links to the next scan. You must reverse the whole chain so the last scan becomes the first, and print the reversed sequence of values.
Line 1: an integer n, the number of nodes in the chain.
Line 2: n space-separated integers, the node values from head to tail (this line is empty when n is 0).
Line 1: the number of nodes in the reversed chain. Line 2: the reversed values, space-separated (an empty line when the chain is empty).
Example 1
Input
4 7 1 3 9
Expected
4 9 3 1 7
Explanation
Reversing the chain 7->1->3->9 gives 9->3->1->7.
Example 2
Input
1 42
Expected
1 42
Explanation
A single-node chain reversed is itself.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →