title: Optimal ways to Traverse through a LinkedList - Java
body: <p>The Situation </p>
  
  <p>I have a interview with TripAdvisor tomorrow and I decided for practice to create my own custom LinkedList. I'm trying to figure out the best way to traverse through it.</p>
  
  <p>Primary Question: I have managed to traverse through my Linked List however I believe
  there is a better way to do it. How would you traverse through it ?</p>
  
  <p>Bonus Question: How do my overall classes look ? Is there anything I should/should not add ?
  It seems to work fine but is it optimal ?</p>
  
  <p>Bonus Question #2: Lastly I was wondering if anyonehad any insight to typical interview questions/concepts that I must know ?</p>
  
  <p>Greatly appreciated.</p>
  
  <p>Here are my Classes</p>
  
  <pre><code>// *********************************Node Class*******************************************     
   public class Node&lt;T&gt; {
    Node&lt;T&gt; link;
  
    T data;
  
    public Node(T data) {
  
      this.data = data;
      link = null;
  
  }
  
  public T getData() {
      return data;
  
  }
  
  public Node&lt;T&gt; getLink() {
  
      return link;
  
  }
  
  
  public Node&lt;T&gt; setLink(Node&lt;T&gt; N) {
  
      this.link = N;
      return link;
  
  }
  
  public void setData(T newData) {
  
      this.data = newData;
  
  }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>    //****************************************Linked List Class*******************************
  
     public class LinkedList&lt;T&gt; {
  
  Node&lt;T&gt; head;
  T data;
  
  
  public LinkedList(){
     head = null;
     }
  
  
  
  
  public void add(T data){
  
      Node&lt;T&gt; newNode = new Node&lt;T&gt; (data);
      newNode.setLink(head);
      head = newNode;
  }
  
  
    //had problems printing out the data in the last node
  
   public void traverse(){
      Node&lt;T&gt; pointer;
      pointer = head;
  
  while (pointer.getLink()!=null){
          System.out.println(pointer.getData());
          pointer = pointer.setLink(pointer.getLink());
  }
  
  //Fixed problems For last node that doesnt get printed out
  System.out.println(pointer.getData());
  
  }
  </code></pre>
  
  <p>//Again is there a better way to do this ?
     //Thanks
  }</p>