<p>There is an integer array <code>nums</code> that consists of <code>n</code> <strong>unique </strong>elements, but you have forgotten it. However, you do remember every pair of adjacent elements in <code>nums</code>.</p>

<p>You are given a 2D integer array <code>adjacentPairs</code> of size <code>n - 1</code> where each <code>adjacentPairs[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that the elements <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> are adjacent in <code>nums</code>.</p>

<p>It is guaranteed that every adjacent pair of elements <code>nums[i]</code> and <code>nums[i+1]</code> will exist in <code>adjacentPairs</code>, either as <code>[nums[i], nums[i+1]]</code> or <code>[nums[i+1], nums[i]]</code>. The pairs can appear <strong>in any order</strong>.</p>

<p>Return <em>the original array </em><code>nums</code><em>. If there are multiple solutions, return <strong>any of them</strong></em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

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

<p><strong class="example">Example 2:</strong></p>

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

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> adjacentPairs = [[100000,-100000]]
<strong>Output:</strong> [100000,-100000]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>nums.length == n</code></li>
	<li><code>adjacentPairs.length == n - 1</code></li>
	<li><code>adjacentPairs[i].length == 2</code></li>
	<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>5</sup> &lt;= nums[i], u<sub>i</sub>, v<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
	<li>There exists some <code>nums</code> that has <code>adjacentPairs</code> as its pairs.</li>
</ul>

class Solution:
    def restoreArray(self, adjacentPairs: List[List[int]]) -> List[int]:
        ll_records = {}

        class LL:
            def __init__(self, value):
                self.value = value
                self.ll_values = []
                self.ll_links = []

            def add_link(self, ll):
                if ll in self.ll_links or ll.value in self.ll_values:
                    return

                if ll.value in ll_records:
                    self.ll_links.append(ll_records[ll.value])
                else:
                    self.ll_links.append(ll)
                    ll_records[ll.value] = ll

                self.ll_values.append(ll.value)

        for pair in adjacentPairs:
            if pair[0] in ll_records:
                ll_1 = ll_records[pair[0]]
            else:
                ll_1 = LL(pair[0])

            if pair[1] in ll_records:
                ll_2 = ll_records[pair[1]]
            else:
                ll_2 = LL(pair[1])

            ll_1.add_link(ll_2)
            ll_2.add_link(ll_1)

        output_ll_list = []

        def build_list(ll, previous, direction):
            if direction == "left":
                output_ll_list.insert(0, ll)
            elif direction == "right":
                output_ll_list.append(ll)

            temp_list = ll.ll_links
            temp_list.remove(previous)
            if len(temp_list) < 1:
                return
            next_ll = temp_list[0]

            build_list(next_ll, ll, direction)


        first_non_end_ll = next(iter(ll_records.values()))
        for ll in ll_records.values():
            if len(ll.ll_links) > 1:
                first_non_end_ll = ll
                break

        output_ll_list.append(first_non_end_ll)
        build_list(first_non_end_ll.ll_links[0], first_non_end_ll, "left")
        if len(first_non_end_ll.ll_links) > 1:
            build_list(first_non_end_ll.ll_links[1], first_non_end_ll, "right")

        output_list = []

        for ll in output_ll_list:
            output_list.append(ll.value)

        return output_list

87785