Question: what needs to be done for python remove method for doubly linked list def remove ( self , index ) : # remove node at

what needs to be done for python remove method for doubly linked list
def remove(self, index): # remove node at specified index
s = self.size() # get size
if s <= index or index <0: # check if the index position is valid
raise IndexError("Index out of range") # error Message
currentNode = self.first # start from first node
if index ==0: # if removing first node,
self.first = self.first.next # set the first node to be 2nd node
self.first.previous = None #The new first node has no previous node, so its previous is None
else:
for i in range (index-1): # go to node just before node being removed
currentNode = currentNode.next # move to the next node
if currentNode.next is not None: # if there is a node to remove
currentNode.next = currentNode.next.next # set the current nodes pointer to skip the node being removed
else:
currentNode.next = None # if its the last node, set the pointer to none

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!