A warehouse cart chain is modeled as a singly linked list of parcel weights, front to back. Swap the weights carried by every adjacent pair of carts: the 1st and 2nd carts swap weights, the 3rd and 4th swap weights, and so on. If the chain holds an odd number of carts, the last (unpaired) cart keeps its original weight. Print the resulting weights, front to back.
Line 1: an integer n — the number of carts.
Line 2: n space-separated integers — the weights, front to back.
n space-separated integers — the weights after swapping adjacent pairs.
Example 1
Input
4 1 2 3 4
Expected
2 1 4 3
Explanation
Swap (1,2) -> 2,1 and swap (3,4) -> 4,3, giving 2 1 4 3.
Example 2
Input
5 10 20 30 40 50
Expected
20 10 40 30 50
Explanation
Swap (10,20) and (30,40); 50 is unpaired and stays last: 20 10 40 30 50.
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 →