Create Empty Linked List Node in Python (Example)
The post is structured as follows:
Let’s dig in!
First, let’s create an object class specifically designed for our linked list nodes. We’ll call this class
In a linked list, each node plays a crucial role in holding data, as well as establishing a connection to the next node in the list. To represent these core elements, we will define two attributes within the
class LLNode: # create linked list node class def __init__(self, data = None, next = None): self.data = data self.next = next
None have been assigned as the default initial value for both
next attributes. Then the
next parameters have been set to these attributes.
In the next section, we will create an object instance, which doesn’t hold any data.
As previously shown, the initial attribute values have been set to
None. Therefore, if we create a node instance without any values assigned, it will result in an empty node.
empty_node = LLNode() # create empty linked list node print(empty_node.data) # print empty node # None
As you can see,
empty_node doesn’t contain any data. If we wish that it had one, we could override the
data attribute, like
LLNode(data = new value).
Video & Further Resources
Do you need further information on the contents of the present article? Then you may watch the following video on my YouTube channel. In the video, I illustrate the Python code of this article:
The YouTube video will be added soon.
Besides the video, you may take a look at the related tutorials that I have published on my website. I have released numerous articles already:
- Find Length of Linked List in Python
- Create Empty 2D List in Python
- Delete Node in Linked List in Python
- Check if Linked List is Empty in Python
- Python Programming Examples
To summarize: This article has shown how to initialize an empty linked list node in Python. Please let me know in the comments, if you have additional questions.
This page was created in collaboration with Cansu Kebabci. Take a look at Cansu’s author page to get more information about her professional background, a list of all his tutorials, as well as an overview on her other tasks on Statistics Globe.