With the Node
in hand, we can start building the actual linked list. Depending on the end-use of the linked list, a variety of methods can be defined.
For our use, we want to be able to:
- get the head node of the list (it’s like peeking at the first item in line)
- add a new node to the beginning of the list
- print out the list values in order
- remove a node that has a particular value
Let’s get started!
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.
Instructions
Within script.py in the pane to the right, create an empty LinkedList
class.
Define an .__init__()
method for the LinkedList
. We want to be able to instantiate a LinkedList
with a head node, so .__init__()
should take value
as an argument. Make sure value
defaults to None
if no value is provided.
Inside the .__init__()
method, set self.head_node
equal to a new Node
with value
as its value.
Define a .get_head_node()
method that helps us peek at the first node in the list.
Inside the method, return the head node of the linked list.