title: Remove method for linkedList implementation in Java
body: <p>I have this method from lecture on removing elements from <code>linkedList</code> at <code>specified index</code>.
  I understand how the method works, but I do not understand why the <code>for-loop</code> leaves the <code>current node pointer</code> two index before the desired index.</p>
  
  <p>Here is the method:</p>
  
  <pre><code>public void remove(int index) {
          if (index == 0) {
              // removing the first element must be handled specially
              front = front.next;
          } else {
              // removing some element further down in the list;
              // traverse to the node before the one we want to remove
              ListNode current = front;
              for (int i = 0; i &lt; index - 1; i++) {
                  current = current.next;
              }
  
              // change its next pointer to skip past the offending node
              current.next = current.next.next;
          }
      }
  </code></pre>
  
  <p>The <code>for-loop</code> goes from <code>0 to &lt; index-1</code>, while I thought it should go from <code>0 to &lt; index</code>. In this way, the pointer is at one <code>index</code> before the <code>index</code> that needed to be deleted. However, the above method works fine.</p>
  
  <p>For eg: 
  in the below <code>LinkedList</code> <img src="https://i.stack.imgur.com/bbaET.png" alt="enter image description here"></p>
  
  <p>Lets consider removing <code>Node C</code>. By the above loop-construct, <code>current pointer</code> will be pointing at <code>Node A</code> and <code>current.next</code> will be <code>Node B</code>. <code>current.next.next</code> will be <code>Node C</code>. Doing <code>current.next=current.next.next</code> will result in <code>Node B</code> deletion rather than <code>Node C</code>.</p>
  
  <p>I think something is wrong with my understanding, can somebody explain?</p>