FAQs on the exercise Linked Lists Implementation II
There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
Alright, so visualizing this and hopefully trying to answer my own question:
Nodes actually look like this ( # is a place holder for value, and I’ll use different brackets to help with visualization):
blah_name = Node [ # , {#, (#, None) } ]
The head node contains all the nodes in it’s 2nd argument. So when :
current_node = current_node.get_next_node()
gets the None value, does that equate to false, thus stopping the while loop? But the if statement is checking for the None. Can someone try to break it down for me what the while loop is doing in this scenario?
This exercise was a struggle for me, partially because the instructions are a bit too abstract and unclear. First, although the directions only say to uncomment the print statement at the end, you actually have to uncomment ALL the statements for it to run correctly.
As for the code itself, it helped me to write out the process for stringify_list() in pseudocode first:
def stringify_list(self):
# create empty string to hold values
string = ""
# keep track of the current node, starting with the head node
current_node = self.get_head_node()
# while we're not at the end of the linked list:
while current_node:
# get the value of the current node
# convert that value to a string
# add it to the string variable created in the beginning (with a new line)
string += str(current_node.get_value()) + "\n"
# move to the next node
current_node = current_node.next_node
# once we reach the end of the linked list, return the full string
return string
Nitpick, maybe, but isn’t it customary Python procedure to name as __str__() the method we have coded and named stringify()? That way, it can be invoked by simply calling list_name.print().
I struggled with the second to last step a lot with this one. Is there any way I could condense this code? I feel like I’m adding a lot of extra steps into it for no reason but I’m struggling to figure out how to make it better.
def stringify_list(self):
string_list =
current_node = self.head_node
while current_node:
if current_node.get_value != None:
string_list.append(str(current_node.get_value()))
current_node = current_node.get_next_node()
stringified = ‘\n’.join(string_list)
return stringifiedPreformatted text
string_list = “”
current_node = self.get_head_node() #get the head_node as the current node
while current_node:
if current_node.get_value() != None:
string_list += str(current_node.get_value()) + "\n"
#append the value of the nodes aleady put in the list (traverse) and transfer them into strings.
current_node = current_node.get_next_node()
This code here is in the while loop, but cannot be placed on the top of if loop. When getting the next node, there is no value in the next node because you don’t use get_value function, so the value will be “None”. Then the console will show an error because “AttributeError: ‘NoneType’ object has no attribute ‘get_value’”.
Therefore, the logic here is to check the value in the current node and then repeatedly check the next node. And check the value in the nodes. Then print out.
This code can be put into the end of the if loop The logic is the same when in the if loop or the while loop.
My understanding of the while statement is that it takes a boolean object, i.e. an object with a truth value of True or False. If the object’s truth value is True, then the loop body is executed. If it is False, then the loop body is not executed.
In this example, it seems to me that the current_node object in the while current_node: line is trivially true. According to the Python documentation, every object has a default truth value of True, apart from a few exceptions (e.g. None, False, 0). Since the truth value of current_node is True, the ensuing loop body is executed. The current_node object functions as a placeholder for the ensuing if statement to do the heavy lifting, if you will.
This helps to explain why there is some confusion about the while current_node: line. The course has not introduced a usage context for the while statement in which the boolean object it takes has a trivial truth value.
…and as I was pondering this issue I reached a further insight that others in this forum have alluded to.
Eventually, the while statement will have iterated through all of the nodes, and therefore current_node.get_next_node() will return None. Since current_node is set to be equal to current_node.get_next_node(), eventually the value of current_node will be set to None. When this happens, the while statement takes the object None in its next iteration, closing the loop, since the truth value of None is False.
Thanks for posting. That’s exactly what I realized by the second time I posted. Plus the notion of a node as a dictionary, which I hadn’t thought about, is interesting.
Precisely. This case is a more straightforward than I implied above. In the Graphs & Maps lessons, nodes are indeed dictionaries. But, in the linked Lists lessons, nodes are actually instances of a class, Node. The docs tell us that objects (such as class instances) can have a truth value of False if
its class defines either a __bool__() method that returns False or a __len__() method that returns zero, when called with the object.
… neither of which, the Node class has. But, as you point out, the code actually assigns the variable current_node the value None, effectively flipping the “while” switch.