Remove Even nodes from a doubly linked list in python

Python program for Remove Even nodes from a doubly linked list. Here problem description and other solutions.

``````#  Python 3 program for
#  Delete all the even nodes from a Doubly Linked List
def __init__(self, data) :
self.data = data
self.next = None
self.prev = None

def __init__(self) :

#  Insert new node at end position
def insert(self, value) :
#  Create a node
return

#  Find last node
while (temp.next != None) :
#  Visit to next node
temp = temp.next

#  Add node at the end position
temp.next = node
node.prev = temp

#  Display node element of doubly linked list
def display(self) :
else :
#  Get first node of linked list
last = None
while (temp != None) :
last = temp
#  Display node value
print("  ", temp.data, end = "")
#  Visit to next node
temp = temp.next

#  Get last node of linked list
temp = last
while (temp != None) :
#  Display node value
print("  ", temp.data, end = "")
#  Visit to prev node
temp = temp.prev

#  Delete all Even element
def deleteEvenNodes(self) :
# when linked list is empty
print("Empty linked list", end = "")
else :
hold = None
while (node != None) :
#  Check node value is even or not
if (node.data % 2 == 0) :
#  When delete first node

if (node.next != None) :
#  When next node present
node.next.prev = node.prev

if (node.prev != None) :
#  When previous node present
node.prev.next = node.next

hold = node
#  Visit to next node
node = node.next
hold.next = None
hold.prev = None
hold = None
else :
#  Visit to next node
node = node.next

def main() :
#  Insert following linked list nodes
dll.insert(8)
dll.insert(1)
dll.insert(4)
dll.insert(9)
dll.insert(5)
dll.insert(3)
dll.insert(12)
print("Before delete even nodes ")
# display all node
dll.display()
print("\nAfter delete even nodes ")
dll.deleteEvenNodes()
dll.display()

if __name__ == "__main__": main()``````

Output

``````Before delete even nodes
8   1   4   9   5   3   12
12   3   5   9   4   1   8
After delete even nodes