title: Delete the first occurrence of an item in a linked list
body: <p>I am trying to delete the first occurrence of an item from a linked list using <code>list.first</code> and <code>list.next</code> only. (This is not using Java <code>LinkedList&lt;E&gt;</code>.)</p>
  
  <p>I have written the following code, but it deletes all occurrences instead of just the first one:  </p>
  
  <pre><code>private static &lt;T&gt; Cell&lt;T&gt; remove(T n,Cell&lt;T&gt; list)
  {
     Cell&lt;T&gt; ptr;  // pointer
     Cell&lt;T&gt; temp=null; // new empty list
     for(ptr=list; ptr!=null; ptr=ptr.next) {
       if(!ptr.first.equals(n))
         temp=new Cell&lt;T&gt;(ptr.first,temp); //I guess this is the problem
     }
     if(ptr!=null)
       ptr=ptr.next;
     for(; temp!=null; temp=temp.next)
       ptr=new Cell&lt;T&gt;(temp.first,ptr);
     return ptr;
  }
  </code></pre>
  
  <p>If <code>n</code> equals <code>3</code> and <code>list</code> equals <code>[1,3,4,5,3,2]</code>, the result I get is <code>[1,4,5,2]</code>, but I want it to be <code>[1,4,5,3,2]</code>.  </p>
  
  <p>Can someone help me understand what I am doing wrong?</p>