<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>

def get_node_from_value(value, value_to_node_mapping):
    existing_node = value_to_node_mapping.get(value)
    if existing_node is not None:
        return existing_node
    new_node = Node(value)
    value_to_node_mapping[value] = new_node
    return new_node


def update_pending_nodes(node, pending_nodes):
    if node.is_linked():
        pending_nodes.remove(node)
    else:
        pending_nodes.add(node)


class Solution:
    def restoreArray(self, adjacentPairs: List[List[int]]) -> List[int]:
        pending_nodes = set()
        value_to_node_mapping = dict()
        for value1, value2 in adjacentPairs:
            node1 = get_node_from_value(value1, value_to_node_mapping)
            node2 = get_node_from_value(value2, value_to_node_mapping)
            node1.add_neighbor(node2)
            node2.add_neighbor(node1)
            update_pending_nodes(node1, pending_nodes)
            update_pending_nodes(node2, pending_nodes)
        result = []
        starting_node = pending_nodes.pop()
        for node in starting_node:
            result.append(node.value)
        return result


class Node:
    def __init__(self, value):
        self.neighbor1 = None
        self.neighbor2 = None
        self.value = value

    def add_neighbor(self, neighbor):
        if self.neighbor1 is None:
            self.neighbor1 = neighbor
            return
        self.neighbor2 = neighbor

    def is_linked(self):
        return self.neighbor2 is not None

    def __iter__(self):
        return NodeIterator(self)


class NodeIterator:
    def __init__(self, node):
        self.__last_picked_node = None
        self.__current_node = node

    def __iter__(self):
        return self

    def __next__(self):
        if self.__current_node is None:
            raise StopIteration

        if self.__current_node.neighbor1 != self.__last_picked_node:
            next_node = self.__current_node.neighbor1
        else:
            next_node = self.__current_node.neighbor2
        self.__last_picked_node = self.__current_node
        self.__current_node = next_node
        return self.__last_picked_node

66335