Does the remove_node() method require a special case for handling removing the last node in the list?


#1

Question

In this exercise, the implementation for remove_node() has a special case for handling removing the first item in the linked list. Does the function also need a special case for removing the last element in the list?

Answer

NO. Removing the last element in the list can be handled by the same code which removes any other item in the list. Only the removal of the first item in the list requires a special case because the value of self.head_node must be updated. The last element in the list does not require special handling because when a Node is created, the value for self.next_node will default to None. So when the remove_node() method calls get_next_node() to update the list, the value of None will be assigned to the next node link for the prior to last element in the linked list thus terminating the list again.