title: How to insert an item at a given position in a linked list?
body: <p>This how you would add an item:</p>
  
  <pre><code>public void insert (Object item)
  {
      Link add = new Link();
      add.data = item;
      add.next = head;
      _head = add;
      ++_listsize;
   }
  </code></pre>
  
  <p>But how do you add an item at a given position. So far this is what I got:</p>
  
  <pre><code>public void insert (Object item, int pos)
  {
      Link add = new Link();
      int ix = pos - 1;
      add.next = _head;
      for (int i = _listsize - 1; i &gt;= ix; --i)
          add = add.next;
      add.data = item;
      _head = add;
     ++_listsize;
  
   }
  </code></pre>
  
  <p>This will insert the item correctly if it is sequential, but let say I am given a position which is in the middle, what it will do it will insert the item but it will completely cut off (or delete the rest). For example:</p>
  
  <p>insert at 1:
  a</p>
  
  <p>insert at 2:
  b
  a</p>
  
  <p>insert at 3:
  c
  b
  a</p>
  
  <p>insert at 2:
  d
  a</p>