What is a List Node in Python? (2 Examples)

 

Hi! This short tutorial will demonstrate what a list node is in the Python programming language.

A list node is typically used in the context of linked list data structure, where each node contains a value and a reference to the next node in the list.

The table of content is structured as follows:

Let’s dive into Python code!

 

Create Sample Linked List Structure

Here, we will create the skeleton of the linked list to demonstrate the use of list nodes. Therefore, in your preferred Python IDE, run the code below to create the sample linked list structure:

class ListNode:
  def __init__(self,val=0,next=None):
    self.val = val
    self.next = next
 
# instantiate the nodes
node1 = ListNode()
node2 = ListNode()
node3 = ListNode()
node4 = ListNode()
 
# link the nodes
node1.next = node2
node2.next = node3
node3.next = node4

The above skeleton is what we will adapt to the following examples to demonstrate the function of list nodes in a linked list data structure.

 

Example 1: Linked List of Integers

In this first example, we will demonstrate a linked list of integers. So, run the code below to create the linked list of integers:

class ListNode:
  def __init__(self, val=0, next=None):
    self.val = val
    self.next = next
 
# instantiate the nodes
node1 = ListNode(1)
node2 = ListNode(2)
node3 = ListNode(3)
node4 = ListNode(4)
 
# link the nodes
node1.next = node2
node2.next = node3
node3.next = node4
 
# traverse the linked list and print each node's value
current_node = node1
while current_node is not None:
    print(current_node.val)
    current_node = current_node.next
 
 
# 1
# 2
# 3
# 4

In the above example, we first created a Python class called “ListNode” with a val attribute to store the node’s value, and a next attribute to store a reference to the next node in the list. The next attribute was initially set to None, indicating that the node had no successor in the list.

The while loop then traversed the linked list starting from the head node “node1” and printed each node’s value until the end of the list was reached, when “current_node” was None.
 

Example 2: Linked List of Strings

In this example, we will follow the same pattern and create a linked list of strings like so:

class ListNode:
  def __init__(self, val=0, next=None):
    self.val = val
    self.next = next
 
# instantiate the nodes
node1 = ListNode("The")
node2 = ListNode("boy")
node3 = ListNode("is")
node4 = ListNode("tall")
 
# link the nodes
node1.next = node2
node2.next = node3
node3.next = node4
 
# traverse the linked list and print each node's value
current_node = node1
while current_node is not None:
    print(current_node.val)
    current_node = current_node.next
 
# The
# boy
# is
# tall

The above example is similar to the previous one; the only difference is that we assigned character strings as values to each node. Thus, when the list was iterated over, the output was a coherent sentence.

This tutorial showed that list nodes are the basic building block of linked lists and facilitate the execution of a process in a logical sequence.

Furthermore, not shown here, they allow for efficient insertion and deletion of elements within a list and can be used to implement various algorithms and data structures, such as queues and stacks.
 

Video, Further Resources & Summary

Do you need more explanations on list nodes in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain list nodes in Python in some more detail.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has explained list nodes in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top