Skip to main content

Doubly Linked List

Insert at beginning

def insertAtBeginning(head, x):
t = Node(x)
if head:
head.prev = t
t.next = head
return t

Insert at end

def insertAtEnd(head, x):
t = Node(x)
if not head:
return t
curr = head
while curr.next:
curr = curr.next
curr.next = t
t.prev = curr
return head

Delete first

def delFirst(head):
if head == None or head.next == None:
return None
head = head.next
head.prev = None
return head

Delete last

def delLast(head):
if head == None or head.next == None:
return None
curr = head
while curr.next.next:
curr = curr.next
curr.next = None
return head