title: Set tail=null when head=null in singly linked list in java?
body: <p>In java singly LinkedList how do I write code that nulls the tail(tail=null) when I null the head(head=null)? I am a beginner and hence find it difficult. I have included if(head==null)in every method but also want to  implement code that sets tail=null when head=null so as to avoid a bug.
  Here is my code</p>
  <pre><code>public class SinglyLinkedList{
    public Node head;
    public Node tail;
    public int size;
  
  
    public Node createLL(int num){
      Node node=new Node();
      node.value=num;
      node.next=null;
      head=node;
      tail=node;
  
      size=1;
      return head;
    }
  
    public void insertNode(int num,int location){
      Node node=new Node();
      node.value=num;
      
      if(head==null){
        createLL(num);
        return;
      }
  
      if(location==0){
        node.next=head;
        head=node;
      }
  
      else if(location&gt;=size){
        node.next=null;
        tail.next=node;
        tail=node;
      }
  
      else{
        Node tempNode=head;
        int index=0;
  
        while(index&lt;location-1){
          tempNode=tempNode.next;
          index++;
        }
       node.next=tempNode.next;
       tempNode.next=node;
      }
      size++;
    }
  
    public void traverse(){
      if(head==null){
        System.out.println(&quot;The linked list is empty&quot;);
      }
      Node tempNode=head;
      for(int i=0;i&lt;size;i++){
        System.out.print(tempNode.value);
        if(i!=size-1){
          System.out.print(&quot;-&gt;&quot;);
        }
        tempNode=tempNode.next;
      }
      System.out.println();
    }
  
    public boolean searchElement(int num){
      Node tempNode=head;
      for(int i=0;i&lt;size;i++){
        if(tempNode.value==num){
          System.out.println(&quot;The value is present at index:&quot;+i);
          return true;
        }
        tempNode=tempNode.next;
      }
      System.out.println(&quot;The value is not present&quot;);
      return false;
    }
  
    public void deleteNode(int location){
      if(head==null){
        System.out.println(&quot;The linked list is not present&quot;);
        return;
      }
  
      else if(location==0){
        head=head.next;
        size--;
        if(size==0){
          tail=null;
        }
      }
  
      else if(location&gt;=size){
        Node tempNode=head;
        for(int i=0;i&lt;size-1;i++){
          tempNode=tempNode.next;
        }
        if(head==null){
          tail=null;
          size--;
          return;
        }
        tempNode.next=null;
        tail=tempNode;
        size--;
        
        head=tail=null;
      }
  
      else{
        Node tempNode=head;
        int index=0;
  
        while(index&lt;location-1){
          tempNode=tempNode.next;
          index++;
        }
        tempNode.next=tempNode.next.next;
        size--;
      }
    }
  
    public void deleteSinglyLinkedList(){
      if(head==null){
        System.out.println(&quot;The linkedlist is absent&quot;);
      }
      head=tail=null;
      System.out.println(&quot;The entire linked list has been deleted&quot;);
    }
  }
  </code></pre>