A single-track playlist is stored as a singly linked list: each node holds an integer and points to the next node. You want to play it back to front, so you must reverse the chain and report the values in the new order (that is, the original list read from tail to head).
Line 1: an integer n, the number of nodes.
Line 2: n space-separated integers, the node values from head to tail (this line is empty when n is 0).
A single line with the n values after reversal, space-separated, from the new head to the new tail. If the list is empty, print EMPTY.
Example 1
Input
5 7 1 9 3 4
Expected
4 3 9 1 7
Explanation
Head-to-tail the values are 7 1 9 3 4. Reversing the chain gives the new order 4 3 9 1 7.
Example 2
Input
0
Expected
EMPTY
Explanation
There are no nodes, so the reversed list is still empty and we print EMPTY.
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 →