isEmpty() is used to check whether the linked list is empty or not. In this post, we will discuss the isEmpty() method of the Java Linked List class in detail.
- Method declaration – public boolean isEmpty()
- What does it do? It will tell you whether the linked list is empty or not by checking the size of the linked list.
- What does it return? It will return true if the linked list is empty; otherwise false.
Below is the internal implementation of isEmpty() method –
public boolean isEmpty() {
return size() == 0;
}
So, here we can see that the isEmpty() method uses size to see if the linked list is empty.
Code Example
import java.util.LinkedList;
public class Codekru {
public static void main(String[] args) {
LinkedList<Object> filledlinkedList = new LinkedList<>();
filledlinkedList.add("codekru"); //adding element in linked list
System.out.println("is linked list empty? " + filledlinkedList.isEmpty());
LinkedList<Object> emptylinkedList = new LinkedList<>();
System.out.println("is linked list empty? " + emptylinkedList.isEmpty());
}
}
Output –
is linked list empty? false
is linked list empty? true
Time complexity of the isEmpty() method
isEmpty() method internally uses the size() method to determine whether the linked list is empty or not. So, isEmpty() time complexity depends on the complexity of the size() method and size() method time complexity is O(1).
So, the time complexity of the isEmpty() method would also be O(1).
The What If scenarios
Q – What if we try to use the isEmpty() method on a LinkedList containing an empty string?
import java.util.LinkedList;
public class Codekru {
public static void main(String[] args) {
LinkedList<Object> emptyStringlinkedList = new LinkedList<>();
emptyStringlinkedList.add(""); // adding empty string to the linked list
System.out.println("is emptyStringlinkedList empty? " + emptyStringlinkedList.isEmpty());
}
}
Output –
is emptyStringlinkedList empty? false
So, here we can see that the isEmpty() method returned false. Because the emptyStringlinkedList is not empty, it contains a string ( though an empty one ); thus, the linked list size is 1 here.
Q – What if we try using the isEmpty() method on a LinkedList containing a null object?
Here also, the isEmpty() method will return false as the linked list contains an object, though the object itself is null.
public class Codekru {
public static void main(String[] args) {
LinkedList<Object> nullObjectlinkedList = new LinkedList<>();
nullObjectlinkedList.add(null); // adding null to the linked list
System.out.println("is nullObjectlinkedList empty? " + nullObjectlinkedList.isEmpty());
}
}
Output –
is nullObjectlinkedList empty? false
Q – And now, what if we try to use isEmpty() on a null LinkedList altogether?
We will get a NullPointerException as illustrated by the below example.
public class Codekru {
public static void main(String[] args) {
LinkedList<Object> nulllinkedList = new LinkedList<>();
nulllinkedList = null;
System.out.println("is nulllinkedList empty? " + nulllinkedList.isEmpty());
}
}
Output –
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.LinkedList.isEmpty()" because "nulllinkedList" is null
Please visit this link to learn more about the linked list class and its methods.
We hope that you have liked this article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.