Question
For this exercise, can the Node __init__()
method be used in the insert_beginning()
method to point the new Node
to the existing start of the list?
Answer
Yes, since the __init__()
method has a parameter which can set the value for next_node
, the insert_beginning()
can pass the existing value for self.head_node
as the parameter when the new object is created. This would simplify the insert_beginning()
to the following code.
def insert_beginning(self, new_value):
new_node = Node(new_value, self.head_node)
self.head_node = new_node