Create Empty Linked List Node in Python (Example)
In this tutorial, you’ll learn how to create an empty linked list node in Python.
The post is structured as follows:
Let’s dig in!
Creation of Linked List Node Class
First, let’s create an object class specifically designed for our linked list nodes. We’ll call this class LLNode
.
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 LLNode
class, data
and next
.
We will use the __init__ constructor to initialize them with the object instance called as self
.
class LLNode: # create linked list node class def __init__(self, data = None, next = None): self.data = data self.next = next
As seen, None
have been assigned as the default initial value for both data
and next
attributes. Then the data
and next
parameters have been set to these attributes.
In the next section, we will create an object instance, which doesn’t hold any data.
Example: Create Empty Linked List Node
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.