You are given a list of pair connections, where each pair is represented as [start, end]. Your task is to reorder all these given pairs so that they form a continuous chain.
A chain is considered valid when for every consecutive pair in the arrangement, the end value of the previous pair is equal to the start value of the next pair.
The input is guaranteed to be such that at least one correct ordering always exists.
Your goal is to return any one valid arrangement of the pairs.
Input: pairs = [[5,1],[4,5],[11,9],[9,4]]
Output: [[11,9],[9,4],[4,5],[5,1]]
Explanation:
Each pair links perfectly: 9 → 4 → 5 → 1
Input: pairs = [[1,3],[3,2],[2,1]]
Output: [[1,3],[3,2],[2,1]]
Explanation:
3 → 2 → 1
Input: pairs = [[1,2],[1,3],[2,1]]
Output: [[1,2],[2,1],[1,3]]
Explanation:
All transitions follow the required rule.
Accepted:
Submission: