添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Check if Linked List is Empty in Python (2 Examples)

Hi! In this tutorial, I will show you how to check if a linked list is empty in the Python programming language .

First, here is an overview of this tutorial:

What is a Linked List? Example 1: Check if Linked List is Empty (True) Example 2: Check if Linked List is Empty (False) Video, Further Resources & Summary

Let’s dive right into it!

What is a Linked List?

A linked list in Python is a data structure that consists of a sequence of nodes, where each node contains a reference to the next node in the list. It is a dynamic data structure, which means that its size can change during the execution of a program.

Unlike an array, a linked list does not have a fixed size and can grow or shrink as needed. In Python, a linked list can be implemented using the built-in list data type or by creating a custom class to define the node and the list.

Example 1: Check if Linked List is Empty (True)

We will create a custom class to define a node and a list for the demonstration, and then check if the list is empty. If it is empty, it will return True ; but if it is not empty, it will return False .

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
class LinkedList:
    def __init__(self):
        self.head = None
    def is_empty(self):
        return self.head is None
ll = LinkedList()
print(ll.is_empty())
# True

The is_empty() method of the LinkedList class checks if the list is empty or not. It is done by checking if the head attribute of the list is None . If it is, it returns True , indicating that the list is empty. If it is not, it returns False , indicating that the list is not empty.

Example 2: Check if Linked List is Empty (False)

In this example, we will set the head attribute of the ll linked list to a new node object. When is_empty() method is called again, this time it will return False , indicating that the list is not empty.

ll.head = Node(1)
print(ll.is_empty())
# False

So, that’s how to check if a linked list is empty in Python. I hope you found this tutorial helpful.

Video, Further Resources & Summary

Do you need more explanations on how to check if a linked list is empty in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to check if a linked list is empty in Python.

The YouTube video will be added soon.

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

This post has shown how to check if a linked list is empty in Python . In case you have further questions, you may leave a comment below.

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 .

I’m Joachim Schork. On this website, I provide statistics tutorials as well as code in Python and R programming.

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 .