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!
I have a question on the exercise in the Linked List Review page.
It’s about the insert_beginning method, which looks like this when implemented as instructed:
def insert_beginning(self, new_value):
print("Function that takes a value parameter")
new_node = Node(new_value)
new_node.set_next_node(self.head_node)
self.head_node = new_node
I wanted to add an alternative insert_beginning method that directly takes a Node as argument:
def insert_beginning(self, new_node):
print("Function that takes a Node parameter")
new_node.set_next_node(self.head_node)
self.head_node = new_node
To my knowledge, this is method overloading, but for some reason, this doesn’t work.
I tried the following test code:
a = Node(1)
b = Node(2)
d = Node(3)
llist = LinkedList(5)
llist.insert_beginning(d)
llist.insert_beginning(b)
llist.insert_beginning(2)
llist.insert_beginning(a)
which gives some pretty strange results because the code only seems to use the original value method.
I myself am not very proficient in Python, and I only just completed this module.
But upon seeing your question, I tried your method.
It seems to work fine for me, it adds the node fine as a direct argument.
I’m thinking you could achieve what you want with modifying the original method instead, to accept Nodes as direct argument, with a “if isinstance(obj, class)” check
I made and tested the following:
I completed the exercise “4. Linked List Implementation III,” but as I attempt the review exercises, I’m running into unexpected problems in my code.
As you can see, toward the bottom I define four nodes that are linked by definition.
I aim to use the LinkedList .stringify_list() method to create a string from the list of the values of the nodes.
The code doesn’t work as expected. The way my program runs, there is no next node beyond node1, whereas I want the program to keep on going to node2, then node3, then node4.
The output of print(linked.stringify_list()) is just a.
But given how I defined my nodes, I expect:
a b c d
Please help!
class Node:
def __init__(self, value, next_node=None):
self.value = value
self.next_node = next_node
def get_value(self):
return self.value
def get_next_node(self):
return self.next_node
def set_next_node(self, next_node):
self.next_node = next_node
class LinkedList:
def __init__(self, value=None):
self.head_node = Node(value)
def get_head_node(self):
return self.head_node
def insert_beginning(self, new_value):
new_node = Node(new_value)
new_node.set_next_node(self.head_node)
self.head_node = new_node
def stringify_list(self):
string_list = ""
current_node = self.get_head_node()
# I believe this is where the problem lies. When the print() below is executed, it returns 'None'. The output I desire is node2. If (current_node.get_next_node() == None), then my string will not grow beyond "a".
print(current_node.get_next_node())
while current_node:
if current_node.get_value() != None:
string_list += str(current_node.get_value()) + "\n"
current_node = current_node.get_next_node()
return string_list
def remove_node(self, value_to_remove):
current_node = self.get_head_node()
if current_node.get_value() == value_to_remove:
self.head_node = current_node.get_next_node()
else:
while current_node:
next_node = current_node.get_next_node()
if next_node.get_value() == value_to_remove:
current_node.set_next_node(next_node.get_next_node())
current_node = None
else:
current_node = next_node
# Define four nodes to be linked using LinkedList methods.
node4 = Node("d")
node3 = Node("c", node4)
node2 = Node("b", node3)
node1 = Node("a", node2)
# Define a LinkedList with a head node with value "a"
linked = LinkedList("a")
# ATTEMPT to print the string of the linked list. This attempt fails.
print(linked.stringify_list())
The problem is in the way the objects are added to the list. Although you instantiate the Node objects with both a value and a next_node attribute, when they are added to the class LinkedList, only the value attribute is retained.
Therefore, only the last Node object added will remain. You must use the insert_beginning() method to add new nodes, as the Class is currently written. Then the code itself will take care of next_node.
I have another question, though this time I want to know if something I already have working could work even better.
Could my LinkedList method .remove_all() be leaner? Opening and closing two while loops seems slightly clunky to me.
I’m sharing my code for the exercise below.
class Node:
def __init__(self, value, next_node=None):
self.value = value
self.next_node = next_node
def get_value(self):
return self.value
def get_next_node(self):
return self.next_node
def set_next_node(self, next_node):
self.next_node = next_node
class LinkedList:
def __init__(self, value=None):
self.head_node = Node(value)
def get_head_node(self):
return self.head_node
def insert_beginning(self, new_value):
new_node = Node(new_value)
new_node.set_next_node(self.head_node)
self.head_node = new_node
def stringify_list(self):
string_list = ""
current_node = self.get_head_node()
while current_node:
if current_node.get_value() != None:
string_list += str(current_node.get_value()) + "\n"
current_node = current_node.get_next_node()
return string_list
def remove_node(self, value_to_remove):
current_node = self.get_head_node()
if current_node.get_value() == value_to_remove:
self.head_node = current_node.get_next_node()
else:
while current_node:
next_node = current_node.get_next_node()
if next_node.get_value() == value_to_remove:
current_node.set_next_node(next_node.get_next_node())
current_node = None
else:
current_node = next_node
def remove_all(self, value_to_remove):
# First while loop: check if head node is to be removed.
# If so, remove, and reset head node to next node.
# Iterate until (self.head_node.get_value() != value_to_remove)
head = self.get_head_node()
while head:
if head.get_value() == value_to_remove:
self.head_node = head.get_next_node()
head = self.head_node
else:
head = None
# Now the computer knows the head node is not to be removed.
# Second while loop: iterate through the nodes in the list.
# If there is no next node:
# Return 'None' to close the loop.
# Else, if the next node has the value to be removed:
# Set the next node of the current node to be the NEXT
# next node rather than the next node. (Remove next node.)
# Else:
# Move to the next node in the linked list and repeat.
current_node = self.get_head_node()
while current_node:
next_node = current_node.get_next_node()
if next_node == None:
current_node = next_node
elif next_node.get_value() == value_to_remove:
current_node.set_next_node(next_node.get_next_node())
else:
current_node = next_node
ll = LinkedList(5)
ll.insert_beginning(90)
ll.insert_beginning(70)
ll.insert_beginning(70)
ll.insert_beginning(5675)
ll.insert_beginning(90)
ll.insert_beginning(5)
ll.insert_beginning(90)
ll.insert_beginning(90)
print(ll.stringify_list())
ll.remove_all(90)
# Print output after ll.remove_all(90)
print(ll.stringify_list())
Modify remove_node() to return a certain value if it cycles through and never finds value_to_remove (a good idea anyway, to protect from a crash if a non-existent value is fed to it.)
Then in remove_all(), just call remove_node() repeatedly until that value is returned.
I really don’t understand what nodes are for. How can they be used in code. I see the “tree diagrams” online but I don’t understand how a programmer actually uses them for anything useful. How are these truly different than a few lists or dictionaries linked together?
They are list or dictionaries linked together! The “difference” is in the programming techniques you can use with them.
For instance, one of the projects in the course “Basic Graph Search Algorithms” provides a “map” of metro stations consisting of a dictionary keying each station to a list of its adjacent stations. There is also a dictionary of city landmarks keyed to nearest metro stations.
The completed project allows you to type in any two landmarks and get in return the shortest route between them.
You might be able to program that without using any tree-search techniques (monte-carlo simulations of many possible routes, perhaps), but I don’t think it would be as efficient.
inside the while loop, what is the usage of current_node = None? Is it to exit out the while loop? Des it affect anything to the current_node data and next_node? Can I use break instead? Thanks
what’s the difference between using get_methods v.s. self.attribute?
In the case of Linked Lists for example, when is it better / do I need to refer to the next node via self.next_node or via self.get_next_node?
Do best practices change when referring to the value stored in the node as opposed to next nodes?
I think you did a great job and I commend you on going the extra mile by tackling Codecademy’s extended learning challenges. I think this is where we learn the most.
In case you or anyone else is interested, I thought I would share the method I used to code a solution. I wanted to find a way to reuse the existing remove_node() method, so I ended up first writing a method to count the number of nodes that contained a specific value (which might be useful in and of itself), then used that to tell the new remove_node_all() method the number of times to run our existing remove_node() method.
I added the following methods to the LinkedList class:
def count_values(self, value_to_count):
count = 0
current_node = self.get_head_node()
while current_node:
if current_node.get_value() == value_to_count:
count += 1
current_node = current_node.get_next_node()
return count
def remove_node_all(self, value_to_remove):
number_to_remove = self.count_values(value_to_remove)
for i in range(number_to_remove):
self.remove_node(value_to_remove)
I ran it and it seems to work as intended. Here is the code I used for testing:
I thought it’d be easier to add remove all as an option for the existing method:
def remove_node(self, value_to_remove, all_nodes = False):
current_node = self.get_head_node()
if current_node.get_value() == value_to_remove:
self.head_node = current_node.get_next_node()
else:
while current_node:
next_node = current_node.get_next_node()
if next_node and next_node.get_value() == value_to_remove:
current_node.set_next_node(next_node.get_next_node())
if all_nodes and next_node.get_value() != value_to_remove:
current_node = next_node
elif all_nodes and next_node.get_value() == value_to_remove:
continue
else:
current_node = None
else:
current_node = next_node
numbers = LinkedList(1)
numbers.insert_beginning(2)
numbers.insert_beginning(3)
numbers.insert_beginning(2)
numbers.insert_beginning(2)
numbers.insert_beginning(2)
numbers.insert_beginning(3)
numbers.insert_beginning(4)
print(numbers.stringify_list())
numbers.remove_node(4)
print(numbers.stringify_list())
numbers.remove_node(2, True)
print(numbers.stringify_list())
Outputs:
4
3
2
2
2
3
2
1
3
2
2
2
3
2
1
3
3
1
Having 3 twos in a row had my stroking my beard for a while as it left one two behind, eventually I figured out I needed an elif to tame too many twos.