â€£

```
class Node(object):
# Each node has its data and a pointer that points to
# next node in the Linked List and previoous
def __init__(self, data, next_node=None, prev=None):
self.data = data
self.next = next_node
self.prev = prev
class DoublyLinkedList(object):
def __init__(self):
self.head = None
# for inserting at beginning of linked list
def insert_to_front(self, data):
if self.head == None:
node = Node(data)
self.head = node
else:
node = Node(data)
self.head.prev = node
node.next = self.head
self.head = node
# for inserting at end of linked list
def insert(self, data):
node = Node(data)
curr = self.head
while(curr.next != None):
curr = curr.next
curr.next = node
node.prev = curr
# deleting a node from linked list
def delete(self, data):
curr = self.head
if(curr.next != None):
# if head node is to be deleted
if(curr.data == data):
curr.next.prev = None
self.head = curr.next
curr.next = None
return
else:
while(curr.next != None):
if(curr.data == data):
break
curr = curr.next
if(curr.next):
# if element to be deleted is in between
curr.prev.next = curr.next
curr.next.prev = curr.prev
curr.next = None
curr.prev = None
else:
# if element to be deleted is the last element
curr.prev.next = None
curr.prev = None
return
if (curr == None):
return
```