title: Concept of Inserting at last in LinkedList
body: <p>I have <code>Link1 class</code> to create the link and display link data,
  <code>LinkedList1</code> to create the list and inserting elements in LinkedList.And <code>singleLinkedList</code> class to call the methods of <code>addFirst</code> and <code>addLast.</code>
  Implementation details are below.<strong>The only issue is in the pointer which should be set for the last link after inserting data at the last element in <code>addlast</code> method.</strong></p>
  
  <p><strong>Link1 class implementation</strong></p>
  
  <pre><code>class Link1{
  
      public String str;
      public Link1 forward;
  
      public Link1(String str){
          this.str=str;
          this.forward=null;
      }
      public void displayLink1(){
          System.out.println("Link DATA::"+str);
      }
  }
  </code></pre>
  
  <p><strong>LinkList1 class implementation</strong></p>
  
  <pre><code>class LinkedList1{
      public Link1 first;
      public LinkedList1(){
          first=null;
      }
      public boolean isEmplty(){
          return first==null;
      }
      public void addFirst(String str){
          Link1 newLink=new Link1(str);
          newLink.forward=first;
          first=newLink;
      }
      public Link1 deleteFirst(){
          Link1 temp=first;
          first=first.forward;
          return temp;
      }
      public void insertLast(String str){
          Link1 current=first;
          Link1 last=first;
          Link1 newLinkLast=new Link1(str);
          while(current!=null){   
          current=current.forward;
              if(current==null);
              {
              last=current.forward;            //trying to reach to end of the linkedlist     
              break;
              }
          }
            newLinkLast.forward=last;                      //the last.displayLink is having the correct data evertime this is method getting called.But this is not going to my LinkedList
      //COMMENTED****last.forward=newLinkLast              //SOMETHING IS MESSED HERE,if i add one works perfect but the whole list gets messed up,including the elements added by insertFirst            
             last=newLinkLast;               
          last.displayLink1();                      //Just for testing i am printing the value of this link,which is coming correct everytime i call this method.
      }
      public void displayOB(){
          Link1 current=first;
          while(current!=null){
              current.displayLink1();
              current=current.forward;
          }
      }
  }
  </code></pre>
  
  <p><strong>singleLinkedList implementation</strong></p>
  
  <p>public class singleLinkedList {</p>
  
  <pre><code>    public static void main(String args[]){
          LinkedList1 ll=new LinkedList1();
  
          ll.addFirst("A");
          ll.addFirst("B");
          ll.addFirst("C");
  
          ll.addFirst("d");
          ll.addFirst("e");
          ll.addFirst("f");
  
          ll.addFirst("x");
          ll.addFirst("y");
          ll.addFirst("z");
  
      ll.insertLast("Insert me at End 1");
      ll.insertLast("Insert me at End 2");
      ll.insertLast("Insert Again");
  
          ll.displayOB();
      }
  }
  </code></pre>
  
  <p><strong>OUTPUT</strong>
   <strong>for 1st line which is newLinkLast.forward=last</strong></p>
  
  <pre><code>Link DATA::Insert me at End 1     //this is the displayLink method called in insertLast
  
  Link DATA::Insert me at End 2     //this is the displayLink method called in insertLast
  
  Link DATA::Insert Again           //this is the displayLink method called in insertLast
  
  Link DATA::z
  Link DATA::y
  Link DATA::x
  Link DATA::f
  Link DATA::e
  Link DATA::d
  Link DATA::C
  Link DATA::B
  Link DATA::A
  </code></pre>
  
  <p><strong>for 2nd COMMENTED LINE line which is newLinkLast.forward=last</strong></p>
  
  <pre><code>Link DATA::Insert me at End 1            //this is the displayLink method called in insertLast
  Link DATA::Insert me at End 2            //this is the displayLink method called in insertLast
  
  Link DATA::Insert Again                  //this is the displayLink method called in insertLast
  
  Link DATA::z
  Link DATA::y
  Link DATA::x
  Link DATA::Insert Again
  </code></pre>
  
  <p><strong>Results Expected</strong>
  the insert last should insert element at last any number of times.</p>