So far we can:
- create a new
LinkedList
using.__init__()
- get the head node of the list using
.get_head_node()
Next up, we’ll define methods for our LinkedList
class that allow us to:
- insert a new head node
- return all the nodes in the list as a string so we can print them out in the terminal!
Instructions
Define an .insert_beginning()
method which takes new_value
as an argument.
- Inside the method, instantiate a
Node
withnew_value
. Name thisnew_node
. - Now, link
new_node
to the existinghead_node
. - Finally, replace the current
head_node
withnew_node
.
Note: Because the workspace is set up with spaces instead of tabs, you will need to use spaces to prevent Python from throwing an error. You can learn more about this here.
Define a .stringify_list()
method we can use to print out a string representation of a list’s nodes’ values.
The method should traverse the list, beginning at the head node, and collect each node’s value in a string. Once the end of the list has been reached, the method should return the string.
You can use str()
to convert integers to strings!
Be sure to add "\n"
between values so that each value prints on a new line.
Test your code by uncommenting the print statement at the bottom of script.py — did your list print what you expected?