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
- If head == NULL
→ return true - 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
- Maintain a variable size in the Linked List class/structure
- When inserting → increment size
- When deleting → decrement size
- 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
