Created
March 4, 2019 22:48
-
-
Save AntonKueltz/a453212d4081d37e3ede1da018db847b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node: | |
def __init__(self, data, prev, next): | |
self.data = data | |
self.prev = prev | |
self.next = next | |
class DoubleLinkedList: | |
def __init__(self): | |
self.head = None | |
self.tail = None | |
def insert(self, data): | |
if self.head is None: | |
self.head = Node(data, None, None) | |
self.tail = self.head | |
else: | |
new_node = Node(data, self.tail, None) | |
self.tail.next = new_node | |
self.tail = new_node | |
return self.tail | |
def delete(self, node): | |
if node == self.head: | |
self.head = node.next | |
if node == self.tail: | |
self.tail = node.prev | |
if node.prev is not None: | |
node.prev.next = node.next | |
if node.next is not None: | |
node.next.prev = node.prev | |
def non_repeat(values): | |
ll = DoubleLinkedList() | |
counts = {} | |
nodes = {} | |
for v in values: | |
if v not in counts: | |
node = ll.insert(v) | |
counts[v] = 1 | |
nodes[v] = node | |
elif counts[v] == 1: | |
node = nodes[v] | |
ll.delete(node) | |
counts[v] = 2 | |
return ll.head.data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment