title: Adding Node to Initially Null LinkedList
body: <p>I'm having issues when I try to add a node to a linked list that is initialized to null. In my method I set a test case to check if the node is initially null and if so it creates a new node with the value that was passed in. But for whatever reason it doesn't work unless the node has atleast one element already passed in. Check it out:</p>
  
  <pre><code>    Node addNode(Node node, int val)
      {
          if(node == null)
              {
                  Node newNode = new Node(val);
                  //node = newNode;
                  return newNode;
              }
          node.next = addNode(node.next, val);
          return node;
      }
  
  //Driver Class
      Scanner in = new Scanner(System.in);
      Node myNode = new Node(1);
      int numEntries = in.nextInt();
      for(int i = 0 ; i &lt; numEntries ; i++)
      {
          int inputVal = in.nextInt();
          myNode.addNode(myNode, inputVal);
      }
  </code></pre>
  
  <p>The above code will not run if myNode is initialized to a null value (Node myNode = null;)</p>
  
  <p>Full Code:</p>
  
  <pre><code>/* package whatever; // don't place package name! */
  
     import java.util.*;
     import java.lang.*;
     import java.io.*;
  
  
  
  
  /* Name of the class has to be "Main" only if the class is public. */
      class Ideone
      {
       public static class Node
       {
           private int value;
           Node next;
           public Node()
           {
               next = null;
           }
           public Node(int val)
           {
              value = val;
              next = null;
          }
          Node addNode(Node node, int val)
          {
              if(node == null)
                  {
                      Node newNode = new Node(val);
                      //node = newNode;
                      return newNode;
                  }
              node.next = addNode(node.next, val);
              return node;
          }
      }
  
      public static void main (String[] args) throws java.lang.Exception
      {
          Scanner in = new Scanner(System.in);
          Node myNode = new Node(1);
          Node current = null;
          Node oddFirst = new Node(1);
          int numEntries = in.nextInt();
          for(int i = 0 ; i &lt; numEntries ; i++)
          {
              int inputVal = in.nextInt();
              myNode.addNode(myNode, inputVal);
          }
          current = myNode;
          while(current != null) // Check if values were copied correctly
          {
              if(oddFirst == null) 
              {
                  oddFirst = new Node(current.value);
              }
              oddFirst.addNode(oddFirst,current.value);
              //oddFirst = current.next;
              //oddFirst = oddFirst.next;
              current = current.next.next;
          }
          while(oddFirst != null)
          {
              System.out.println("Current Value: " + oddFirst.value);
              oddFirst = oddFirst.next;
          }
      }
  }
  </code></pre>