title: Printing elements of a linked list in java
body: <p>I am trying to implement linked list in java. In my main class I get some integers from user and put them in a linked list and then print out my linked list elements. Everything's working fine so far, however I think in my main class, it makes sense to first print out each element's data and then move on to the next element. When I do that, it doesn't print the last element of my list but it prints the first element twice. I decided to first move on to the next element and then print the previous element's data and it works just fine!!! Can anyone explains why?(check out the last two lines of my code).</p>
  
  <pre><code>public class Node {
  Node next;
  int data;
  public Node(int data){
      this.data=data;
  }
  } 
  </code></pre>
  
  <p>My linked list class:</p>
  
  <pre><code>public class LinkedList {
  Node head;
  
  public void append(int data){
      if(head==null){
          head=new Node(data);
      }
      Node current;
      current=head;
      while(current.next!=null){
          current=current.next;
      }
      current.next=new Node(data);
  }
  }
  </code></pre>
  
  <p>My main class:</p>
  
  <pre><code>public class Main {
  static LinkedList linkedList =new LinkedList();
  public static void main(String [] args){
      System.out.println("please enter numbers you wanna store in a linked list");
      Scanner scanner=new Scanner(System.in);
      while (scanner.hasNextInt()){
          linkedList.append(scanner.nextInt());
      }
      if (linkedList.head!=null){
          Node current;
          current=linkedList.head;
          while (current.next!=null){
              **current=current.next;
              System.out.println(current.data);**
          }
      }
  }
  }
  </code></pre>