title: Add node to end of Linked List
body: <p>Having a bit of trouble adding a node to the end of my linked list. It only seems to display the very last one I added before I call my addFirst method. To me it looks like on the addLast method I'm trying to first create the node to assign it 5, then for the following numbers use a while loop to assign them to the last node on the linked list. Little stuck on why I can't get my output to display 5 and 6.</p>
  
  <pre><code>class LinkedList
  {
      private class Node          
      {
      private Node link;
      private int x;
      }
      //----------------------------------
      private Node first = null;    
      //----------------------------------
      public void addFirst(int d)
      {
          Node newNode = new Node();
          newNode.x = d;            
          newNode.link = first;    
          first = newNode;           
      }
      //----------------------------------
      public void addLast(int d)
      {
          first = new Node();
          if (first == null)
          {
              first = first.link;
          }
          Node newLast = new Node();
          while (first.link != null)
          {
              first = first.link;
          }
          newLast.x = d;
          first.link = newLast;
          first = newLast;
      }
      //----------------------------------
      public void traverse()
      {
          Node p = first;
          while (p != null)           
          {
              System.out.println(p.x);  
              p = p.link;               
          }
      }
  }
  //==============================================
  class test123
  {
      public static void main(String[] args)
      {
          LinkedList list = new LinkedList();
          list.addLast(5);
          list.addLast(6);
          list.addLast(7);
          list.addFirst(1);
          list.addFirst(2);
          list.addFirst(3);
          System.out.println("Numbers on list");
          list.traverse();
      }
  }
  </code></pre>
  
  <p>I've also tried creating a last Node and in the traverse method using a separate loop to traverse the last node. I end up with the same output!</p>
  
  <pre><code>   public void addLast(int d)
     {
         Node newLast = new Node();
         while (last.link != null)
         {
            last = newLast.link;
         }    
         newLast.x = d; 
         newLast.link = last;
         last = newLast;       
     }
  </code></pre>