title: Implementation of removeFirst() method in SLinkedList in java
body: <p>I got the following code from one book for implementing a singly linked list. And I don't understand some lines of code in the <code>removeFirst()</code> method, which removes the first node from the LinkedList.</p>
  
  <pre><code>class ListNode{
      private String element;
      private ListNode next;
  
      public ListNode(){
          element = null;
          next = null;
      } 
      public ListNode(String s, ListNode n){
          element = s;
          next = n;
      }
      //Access method
      public String getElement(){
          return element;
      }
      public ListNode getNext(){
          return next;
      }
      //Modify method
      public void setNext(ListNode n){
          next = n;
      }
  }
  
  public String removeFirst(){
      if(head == null)
          return null;
      else{
          ListNode temp = head;
          head = head.getNext();
          temp.setNext(null);   //Which I don't understand, is it necessary?
          size --;
          return temp.getElement();
      }
  }
  </code></pre>
  
  <p>It seems that the statement <code>temp.setNext(null);</code> can be omitted. So why it is here, does it has anything to do with the garbage colletion in java. Since I am new to Java, any suggestions or ideas?</p>