educative.io

Educative

Why add cur_node in if statement (delete node function)

Hello,
I would like to understand why you check if cur_node equals to True in your if statement.
It seems the code would work just fine with just:
if cur_node.data == key:

Thanks a lot
Best regards
Guillaume

def delete_node(self, key):

cur_node = self.head

if cur_node and cur_node.data == key:
self.head = cur_node.next
cur_node = None
return


Course: Data Structures and Algorithms in Python - Learn Interactively
Lesson: Deletion by Value - Data Structures and Algorithms in Python

Hi @Guillaume_Besson,
As mentioned in the lesson we use if cur_node and cur_node.data == key statement to handle the case of deleting the head.
I hope it will help. Please drop us a note if you have any other questions or concerns.