title: Divide a linked list into half and return the second half
body: <p>This was a programming question that I did wrong.There was a partial code give as follows:</p>
  
  <pre><code>public class SingleLinkedList&lt;E&gt; {
     private Node&lt;E&gt; head;
     private int size = 0;
  
     private static class Node&lt;E&gt; {
       private E data;
       private Node&lt;E&gt; next;
  
       /** Creates a new node with a null next field
           @param dataItem  The data stored
       */
       private Node(E data) {
         data = dataItem;
         next = null;
       }
  
      /** Creates a new node that references another node
           @param dataItem  The data stored
           @param nodeRef  The node referenced by new node
       */
       private Node(E dataItem, Node&lt;E&gt; nodeRef) {
         data = dataItem;
         next = nodeRef;
       }
    }
  
  }
  </code></pre>
  
  <p>My task was to create a method that divides the linked list in half leaving the first half of the elements in the original list and returning a new SingleLinkedList containing the second half of the list.If the number of elements is odd, then the extra element should go with the first half.I could not do it.Then my teacher gave answer like the following:</p>
  
  <pre><code>SingleLinkedList&lt;E&gt; newlist = new SingleLinkedList&lt;E&gt;();
   newlist.head =  temp.next; 
  temp.next= null; 
  return newlist
  </code></pre>
  
  <p>However, I'm not even getting the answer.I am a beginner.I would appreciate if someone can explain this problem.</p>