Practice Restore the Array From Adjacent Pairs
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
There is an integer array nums that consists of n unique elements, but you have forgotten it. However, you do remember every pair of adjacent elements in nums. You are given a 2D integer array adjacentPairs of size n - 1 where each adjacentPairs[i] = [ui, vi] indicates that the elements ui and vi are adjacent in nums. It is guaranteed that every adjacent pair of elements nums[i] and nums[i+1] will exist in adjacentPairs, either as [nums[i], nums[i+1]] or [nums[i+1], nums[i]]. The pairs can appear in any order. Return the original array nums. If there are multiple solutions, return any of them.
Source: Restore the Array From Adjacent Pairs on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
adjacentPairs = [[2,1],[3,4],[3,2]]
[1,2,3,4]
# Example 2
adjacentPairs = [[4,-2],[1,4],[-3,1]]
[-2,4,1,-3]
# Example 3
adjacentPairs = [[100000,-100000]]
[100000,-100000]
Constraints
Related LeetCode Problems
Practice more problems similar to Restore the Array From Adjacent Pairs.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.