title: Linked Lists. Head and Tail References
body: <p>Im trying to make an Add and Delete method to my linked list class. I made 2 references with the names Head and Tail. </p>
  
  <p>Head -> 1 -> 2 -> 3 -> Tail:null</p>
  
  <p>I keep getting problems when I try and delete specific nodes because Java says I'm out of bounds. I think it is because my Head isn't pointing to the first Node? What do you guys think? Or am i going about this the total wrong way...</p>
  
  <pre><code>public class LinkedList{
  
      private Node head;
      private Node tail;
      private int listCount;
  
      public LinkedList()
      {
          head = new ListNode(null);
          tail = new ListNode(null);
          listCount = 0;
      }
  
      public void add(Object elem){
          ListNode newNode = new ListNode(elem);
          if (size() == 0){
              newNode = head;
              tail = head;
          }
          newNode.setLink(tail);
          tail = newNode;
          listCount++;
          }
  
      public Object delete(int index)
      // post: removes the element at the specified position in this list.
      {
          // if the index is out of range, exit
          if(index &lt; 1 || index &gt; size())
              throw new IndexOutOfBoundsException();
  
          ListNode current = head;
          for(int i = 1; i &lt; index; i++)
          {
              if(current.getLink() == null)
                  throw new IndexOutOfBoundsException();
  
              current = current.getLink();
          }
          current.setLink(current.getLink().getLink());
          listCount--; // decrement the number of elements variable
          return current.getInfo();
      }
  
      public int size() {
          return listCount;
      }
  
  }
  
  public class Node {
  
      private Node link;
      private Object info;
  
      public Node(Object info)
      {
          this.info = info;
          link = null;
      }
  
      public void setInfo(Object info)
      {
          this.info = info;
      }
  
      public Object getInfo()
      {
          return info;
      }
  
      public void setLink(Node link)
      {
          this.link = link;
      }
  
      public Node getLink()
      {
          return link;
      }
  }
  </code></pre>