Question
For this exercise, the set_next_node()
method will change the value of self.next_node
to the value passed. Should the method have a check to prevent the assignment of None
?
Answer
NO. The set_next_node()
method must allow the assignment of None
. For example, when deleting the last node from a linked list, the previous node must have its next_node
value set to None
to indicate that it is now the end node in the list.
Users might become confused by the difference between the question posed by the title of this post and the question asked within the post.
The title is …
Should the set_next_node in the Node class allow values of None?
… while the question asked within is …
Should the method have a check to prevent the assignment of None
?
So the answer to the question in the title would be YES while the answer to the question is NO, as is given. Users should be alerted to the fact that two opposite questions are being asked here.
On another related note, it might be instructive to think about how misuse of the constructor or the set_next_node()
method could corrupt a LinkedList
. Some type checking might be a good idea, such as …
if next_node != None and type(next_node) != Node:
raise TypeError