Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Your task is to complete this function
- # function should add a new node after the pth position
- # function shouldn't print or return any data
- '''
- class Node:
- def __init__(self, data):
- self.data = data
- self.next = None
- self.prev = None
- '''
- #Function to insert a new node at given position in doubly linked list.
- def addNode(head, p, data):
- # Code here
- temp = Node(data)
- if p == 0:
- temp.next = head.next
- temp.prev = head
- head.next = temp
- else:
- current = head
- for i in range(p):
- current = current.next
- temp.next = current.next
- temp.prev = current
- current.next = temp
- return head
Advertisement
Add Comment
Please, Sign In to add comment