educative.io

Empty Linked List

A learner asked the following question:

Message: Hi, The delete_node() function in LinkedList class has a bug. If LL is empty it should throw an error message. def delete_node(self, key): curr_node = self.head # Print error if empty linked list if curr_node is None: print(“Deleting from empty linked list”) return

Hi,

This is Alina from Educative. Thank you for reaching out to us and giving your feedback.

This is not a bug. One could argue that this is something that could be checked, but the code already throws an error if you try to perform this action. Checking if the linked list is empty and throwing an error would effectively do the same thing that the built-in Python types already do.

Best Regards,
Alina Fatima | Developer Advocate