# This is an input class. Do not edit.
class Node:
    def __init__(self, value):
        self.value = value
        self.prev = None
        self.next = None


# Feel free to add new properties and methods to the class.
class DoublyLinkedList:
    def __init__(self):
        self.head = None
        self.tail = None

    def setHead(self, node):
        if self.head==None:
			self.head=node
			self.tail=node
			return 
		self.insertBefore(self.head,node)

    def setTail(self, node):
        if self.tail==None:
			self.setHead(node)
			return
		
		self.insertAfter(self.tail,node)
		
	
    def insertBefore(self, node, nodeToInsert):
		if nodeToInsert==self.head and nodeToInsert==self.tail:
			return
		self.remove(nodeToInsert)
		nodeToInsert.prev=node.prev
		nodeToInsert.next=node
		if node.prev ==None:
			self.head=nodeToInsert
		else:
			node.prev.next=nodeToInsert
		node.prev=nodeToInsert
		

    def insertAfter(self, node, nodeToInsert):
        if self.head==nodeToInsert and self.tail==nodeToInsert:
			return
		self.remove(nodeToInsert)
		nodeToInsert.prev=node
		nodeToInsert.next=node.next
		if node.next==None:
			self.tail=nodeToInsert
		else:
			node.next.prev=nodeToInsert
		node.next=nodeToInsert

    def insertAtPosition(self, position, nodeToInsert):
        if position==1:
			self.setHead(nodeToInsert)
			return
		node=self.head
		currentPosition=1
		while node is not None and currentPosition!=position:
			node=node.next
			currentPosition+=1
		if node is not None:
			self.insertBefore(node,nodeToInsert)
		else:
			self.setTail(nodeToInsert)

    def removeNodesWithValue(self, value):
        node=self.head
		while node!=None:
			nodeToRemove=node
			node=node.next
			if nodeToRemove.value==value:
				self.remove(nodeToRemove)

    def remove(self, node):
        if node==self.head:
			self.head=self.head.next
		if node==self.tail:
			self.tail=self.tail.prev
		self.removenodebindings(node)
		

    def containsNodeWithValue(self, value):
    	node=self.head
		while node!=None:
			if node.value==value:
				return True
			node=node.next
		return False
	
	def removenodebindings(self,node):
		if node.prev!=None:
			node.prev.next=node.next
			
		if node.next!=None:
			node.next.prev=node.prev
			
		node.next=None
		node.prev=None
			
