Undo the XOR Chain
A secret list holds the numbers 1 to n in some order, and n is odd. You only get links, where links[i] is the XOR (^) of items i and i + 1 of the secret list.
Return the secret list. It is always the only one that fits.
Example 1
links [1,2]
returns [2,3,1]
2 ^ 3 = 1 and 3 ^ 1 = 2. No other order of 1, 2, 3 gives these links.
Example 2
links [5,4,6,1]
returns [4,1,5,3,2]
4 ^ 1 = 5, 1 ^ 5 = 4, 5 ^ 3 = 6 and 3 ^ 2 = 1.
Constraints
2 <= len(links) <= 298, and the length is evenlinkscomes from some order of the numbers 1 tolen(links) + 1
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.