kucheasysa

Algoverse_adesh_41

Jul 10th, 2024
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.72 KB | None | 0 0
  1. # Your task is to complete this function
  2. # function should add a new node after the pth position
  3. # function shouldn't print or return any data
  4.  
  5. '''
  6. class Node:
  7.     def __init__(self, data):
  8.         self.data = data
  9.         self.next = None
  10.         self.prev = None
  11.  
  12. '''
  13. #Function to insert a new node at given position in doubly linked list.
  14. def addNode(head, p, data):
  15.     # Code here
  16.     temp = Node(data)
  17.    
  18.     if p == 0:
  19.         temp.next = head.next
  20.         temp.prev = head
  21.         head.next = temp
  22.        
  23.     else:
  24.         current = head
  25.         for i in range(p):
  26.             current = current.next
  27.        
  28.         temp.next = current.next
  29.         temp.prev = current
  30.         current.next = temp
  31.         return head
  32.  
Advertisement
Add Comment
Please, Sign In to add comment