title: java listnode instance variable
body: <p>I'm trying to make sense of this method called append and prepend but i can't seem to make sense of it without a correct visual picture in my mind. what does last.next really mean? how come you can have an instance variable stuck with another instance variables? as you can see both classes are working together to form the listnode in the linked list. heres the code:</p>
  
  <p>in the linkedlist.java class the code looks like this:</p>
  
  <pre><code>public class LinkedList {
  
      /** First node in linked list - dummy node */
      private ListNode first = new ListNode(null);
  
      /** Last node in linked list */
      private ListNode last = first;
  
      /**
       * Appends a String data element to this LinkedList.
       * 
       * @param data
       *            the data element to be appended.
       */
      public void append(String d) {
          // TODO Code here for append
          ListNode node = new ListNode(d);
          last.next =  node;
  
          last = last.next;
          length++;
  
  
          //last = appendSecondNode;
          //first = appendFirstNode;
  
      } // method append(String)
  
    /**
       * Prepends (adds to the beginning) a String data element to this
       * LinkedList.
       * 
       * @param data
       *            the data element to be prepended.
       */
      public void prepend(String d) {
          // TODO Code here for prepend
          // assigns to d to the first box of ListNode
          // handle first prepend by making it
          // work exactly like append
          if(length==0){
  
              append(d);
              return;
          }
          //create new ListNode to prepend
          ListNode  toAddNode = new ListNode(d);
          //set link from the node to add the previous
          // first element so we don't lose the rest of the list
          toAddNode.next = first.next;
          //update the new first node
          first.next = toAddNode;
          // increment the length of the list
          length++;
  
  
  
      } // method append(String)
  
  in another class we have a listnode.java the code looks like this:
  
  // ListNode.java
  
  public class ListNode
  {
     public String data;
     public ListNode next;
  
     public ListNode(String d)
     {
        data = d;
        next = null;
     }  // constructor
  }  // class ShortNode
  </code></pre>