Reputation: 13
I am a beginner in Java and currently completing a Udemy course on DSA. I am learning linked lists and am working on methods to insert and delete nodes to and from linked lists respectively.
From what I have learnt so far I know that we use condition head==null
to check if the linked list is empty or not.
If the condition head==null
is true then LinkedList is empty else it is not empty.
However, shouldn't we check whether tail==null
as well because the tail will always refer to the last node in the LinkedList even after we make head==null
?
Here is my code:
public class SinglyLinkedList{
public Node head;
public Node tail;
public int size;
//Create Linkedlist
public Node createLL(int num){
Node node=new Node();
node.value=num;
node.next=null;
head=node;
tail=node;
size=1;
return head;
}
//Insert Node
public void insertNode(int num,int location){
Node node=new Node();
node.value=num;
if(head==null){//Used to check if linked list is empty or not?
createLL(num);
return;
}
else if(location==0){
node.next=head;
head=node;
}
else if(location>=size){
node.next=null;
tail.next=node;
tail=node;
}
else{
Node tempNode=head;
int index=0;
while(index<location-1){
tempNode=tempNode.next;
index++;
}
node.next=tempNode.next;
tempNode.next=node;
}
size++;
}
//Delete Node
public void deleteNode(int location){
if(head==null){//Used to check if linked list is empty or not?
System.out.println("The linked list is not present");
return;
}
else if(location==0){
head=head.next;
size--;
if(size==0){
tail=null;
}
}
else if(location>=size){
Node tempNode=head;
for(int i=0;i<size-1;i++){
tempNode=tempNode.next;
}
if(head==null){
tail=null;
size--;
return;
}
tempNode.next=null;
tail=tempNode;
size--;
}
else{
Node tempNode=head;
int index=0;
while(index<location-1){
tempNode=tempNode.next;
index++;
}
tempNode.next=tempNode.next.next;
size--;
}
}
}
Upvotes: 1
Views: 1958
Reputation: 11
Java LinkedList, checking head == null is use for if the list is empty, head is the starting point of the list. If the head is null, it means there are no elements in the list.
Checking tail == null might not be as straightforward because the tail of the list can be null even if there are elements in the list. In a LinkedList, the tail often points to the last node, and its next reference is null. So, having a null tail doesn't necessarily mean the list is empty ,it means that the last node has not added till now.
Upvotes: 0
Reputation: 73578
Empty list:
head -> null
tail -> null
Single node list:
head -> node1
node1.next -> null
tail -> node1
Multi node list:
head -> node1
node1.next -> node2
node2.next -> null
tail -> node2
Where ->
means "reference points to". So there's no need to check for both head/tail for nullness. If either of them is null, it means the list has no nodes and is empty.
Upvotes: 2
Reputation: 188
Nodes in a Linked-List exist as a value and memory address (for the next node) pair as you have implemented in the program above. For the last node, the end of the list is represented as NULL.
We check if the head is NULL since if the memory address of the next node in the linked list is NULL, then that means the end of the list has been reached. The tail of the linked list will always be NULL since it would represent the end of the list, and there would be no next node to point to.
Upvotes: -1