Let’s implement a linked list in Python. As you might recall, each linked list is a sequential chain of nodes. So before we start building out the
LinkedList itself, we want to build up a
Node class in Python that we can use to build our data containers.
Remember that a node contains two elements:
- a link to the next node
Ready? 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.
Within script.py in the pane to the right, create an empty
Inside, define an
__init__() method for the
Node. It should take a
value and a
next_node should default to
None if not provided. These variables should be saved to
self with corresponding key names.
.get_next_node() methods. These should return the corresponding values from
.set_next_node() method that takes
next_node as parameters and allows you to update the link to the next node.
Node class, create an instance of
my_node with a value of 44.
.get_value() to print the value of