Linked List February 22 ,2026

Check if Linked List is Empty

Problem

Given the head of a Linked List, determine whether the list is empty or not.

A linked list is considered empty when the head pointer is NULL (or None).

Return:

  • true if the list is empty
  • false otherwise

Example

Input

Head → NULL

Output

True

Input

10 → 20 → 30

Output

False

Approach 1 — Check Head Pointer

Idea

Since the head always points to the first node, if head is NULL, no nodes exist.

This is the simplest and most efficient method.

Steps

  1. If head == NULL
    → return true
  2. Otherwise
    → return false

Time Complexity

O(1)

Space Complexity

O(1)

C Implementation

// While running this code, replace all #include "..." with #include <...>

#include "stdio.h"
#include "stdlib.h"

struct Node {
    int data;
    struct Node* next;
};

int isEmpty(struct Node* head) {
    return (head == NULL);
}

int main() {
    struct Node* head = NULL;

    if (isEmpty(head))
        printf("True");
    else
        printf("False");

    return 0;
}

Output

True

C++ Implementation

// While running this code, replace all #include "..." with #include <...>
#include "iostream"
using namespace std;

class Node {
public:
    int data;
    Node* next;

    Node(int x) {
        data = x;
        next = NULL;
    }
};

bool isEmpty(Node* head) {
    return head == NULL;
}

int main() {
    Node* head = NULL;

    cout << (isEmpty(head) ? "True" : "False");

    return 0;
}

Output

True

Java Implementation

class Node {
    int data;
    Node next;

    Node(int d) {
        data = d;
        next = null;
    }
}

public class Main {

    static boolean isEmpty(Node head) {
        return head == null;
    }

    public static void main(String[] args) {
        Node head = null;

        System.out.println(isEmpty(head));
    }
}

Output

true

Python Implementation

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def is_empty(head):
    return head is None

head = None

print(is_empty(head))

Output

True

JavaScript Implementation

class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

function isEmpty(head) {
    return head === null;
}

let head = null;

console.log(isEmpty(head));

Output

true

Approach 2 — Using Size Counter (If Maintained)

Idea

In some linked list implementations, a variable called size (or length) is maintained that stores the number of nodes in the list.

So instead of checking the head pointer, we check:

  • If size == 0 → list is empty
  • Otherwise → list is not empty

This approach is commonly used in:

  • Standard library implementations
  • Queue/Deque implementations
  • Production-level data structures

Steps

  1. Maintain a variable size in the Linked List class/structure
  2. When inserting → increment size
  3. When deleting → decrement size
  4. To check empty:
    • If size == 0 → return true
    • Else → return false

Time Complexity

O(1)

Space Complexity

O(1)

C Implementation

// While running this code, replace all #include "..." with #include <...>

#include "stdio.h"
#include "stdlib.h"

struct Node {
    int data;
    struct Node* next;
};

struct LinkedList {
    struct Node* head;
    int size;
};

int isEmpty(struct LinkedList* list) {
    return (list->size == 0);
}

int main() {
    struct LinkedList list;
    list.head = NULL;
    list.size = 0;

    if (isEmpty(&list))
        printf("True");
    else
        printf("False");

    return 0;
}

Output

True

C++ Implementation

// While running this code, replace all #include "..." with #include <...>

#include "iostream"
using namespace std;

class LinkedList {
public:
    int size;

    LinkedList() {
        size = 0;
    }

    bool isEmpty() {
        return size == 0;
    }
};

int main() {
    LinkedList list;

    std::cout << (list.isEmpty() ? "True" : "False");

    return 0;
}

Output

True

Java Implementation

class LinkedList {
    int size;

    LinkedList() {
        size = 0;
    }

    boolean isEmpty() {
        return size == 0;
    }
}

public class Main {
    public static void main(String[] args) {
        LinkedList list = new LinkedList();

        System.out.println(list.isEmpty());
    }
}

Output

true

Python Implementation

class LinkedList:
    def __init__(self):
        self.size = 0

    def is_empty(self):
        return self.size == 0

ll = LinkedList()

print(ll.is_empty())

Output

True

JavaScript Implementation

class LinkedList {
    constructor() {
        this.size = 0;
    }

    isEmpty() {
        return this.size === 0;
    }
}

let list = new LinkedList();

console.log(list.isEmpty());

Output

true

Next Problem in the series-

Copy a Linked List

Sanjiv
0

You must logged in to post comments.

Related Blogs

Introducti...
Linked List January 01 ,2026

Introduction to Link...

Length of...
Linked List January 01 ,2026

Length of a Singly L...

Remove Eve...
Linked List January 01 ,2026

Remove Every K-th No...

Middle of...
Linked List January 01 ,2026

Middle of a Linked L...

Count Occu...
Linked List January 01 ,2026

Count Occurrences in...

Circular L...
Linked List January 01 ,2026

Circular Linked List...

Check if a...
Linked List January 01 ,2026

Check if a Linked Li...

Count Node...
Linked List January 01 ,2026

Count Nodes in a Cir...

Deletion f...
Linked List January 01 ,2026

Deletion from a Circ...

Singly to...
Linked List January 01 ,2026

Singly to Circular L...

Exchange F...
Linked List January 01 ,2026

Exchange First and L...

Deletion i...
Linked List January 01 ,2026

Deletion in a Doubly...

Print a Si...
Linked List February 02 ,2026

Print a Singly Linke...

Search in...
Linked List February 02 ,2026

Search in a Singly L...

Linked Lis...
Linked List February 02 ,2026

Linked List Insertio...

Deleting a...
Linked List February 02 ,2026

Deleting a Given Key...

Deleting a...
Linked List February 02 ,2026

Deleting a Node at a...

Delete Ent...
Linked List February 02 ,2026

Delete Entire Linked...

Nth Node f...
Linked List February 02 ,2026

Nth Node from Start

Nth Node f...
Linked List February 02 ,2026

Nth Node from End

Size of Do...
Linked List February 02 ,2026

Size of Doubly Linke...

Reverse a...
Linked List February 02 ,2026

Reverse a Singly Lin...

Reverse a...
Linked List February 02 ,2026

Reverse a Doubly Lin...

Insert at...
Linked List February 02 ,2026

Insert at Beginning...

Insert at...
Linked List February 02 ,2026

Insert at End of Dou...

Delete Fir...
Linked List February 02 ,2026

Delete First Node of...

Copy a Lin...
Linked List February 02 ,2026

Copy a Linked List

Swap Nodes...
Linked List February 02 ,2026

Swap Nodes in Pairs

Detect Loo...
Linked List February 02 ,2026

Detect Loop in a Lin...

Length of...
Linked List February 02 ,2026

Length of Loop in Li...

Design Bro...
Linked List February 02 ,2026

Design Browser Histo...

Get In Touch

Kurki bazar Uttar Pradesh

+91-8808946970

techiefreak87@gmail.com