#1743

Restore the Array From Adjacent Pairs

pupil · 490 · lc medium +27 · verified · 75.1% accepted · 2,051 likes · top 86%

Description

An integer array nums of n unique elements was lost but every adjacent pair is recorded in adjacentPairs of size n - 1 where adjacentPairs[i] = [ui, vi] means they are neighbors. Recover and return nums; any valid answer is accepted.

Example 1:

Input: adjacentPairs = [[2,1],[3,4],[3,2]]
Output: [1,2,3,4]
Explanation: This array has all its adjacent pairs in adjacentPairs.
Notice that adjacentPairs[i] may not be in left-to-right order.

Example 2:

Input: adjacentPairs = [[4,-2],[1,4],[-3,1]]
Output: [-2,4,1,-3]
Explanation: There can be negative numbers.
Another solution is [-3,1,4,-2], which would also be accepted.

Example 3:

Input: adjacentPairs = [[100000,-100000]]
Output: [100000,-100000]

Code

1
2
3