title: Failing to remove the first node in a LinkedList, but succeed to remove other nodes
body: <p>I am practicing using Java's <code>LinkedList</code>.<br>
  The purpose of the main method here is to remove the given index's node.</p>
  
  <p>The given node can be removed rightly, except the first node.</p>
  
  <p>If I changed <br/> <code>removeLastKthNode(head, lastKth);</code> into <br/> <code>head = removeLastKthNode(head, lastKth)</code>, it works.</p>
  
  <p>But I don't know why <code>removeLastKthNode(head, lastKth)</code> cannot remove the first node.</p>
  
  <p>Andreas gave me a link about this problem
  <a href="https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value">Is Java &quot;pass-by-reference&quot; or &quot;pass-by-value&quot;?</a></p>
  
  <p>However, problem here is that "removeLastKthNode" behaves as "pass by reference" when "lastKth" doesn't refer the first node(head node).</p>
  
  <p>It is sure that "removeLastKthNode" somehow behaves like "pass by reference".
  But why the method "removeLastKthNode" didn't behave like "pass by reference" when "lastKth = arr.length" ?</p>
  
  <p>It is confusing here.</p>
  
  <p>Some results below:<br/>
  when "lastKth = 6"</p>
  
  <pre><code>Initial LinkedList:
  2 4 6 8 11 3 7
  
  After remove last6th LinkedList:
  2 6 8 11 3 7
  </code></pre>
  
  <p>when "lastKth = 7"</p>
  
  <pre><code>Initial LinkedList:
  2 4 6 8 11 3 7
  
  After remove last7th LinkedList:
  2 4 6 8 11 3 7
  </code></pre>
  
  <p>Thanks for sekky's advice. I wrote the code myself and add to the code below.</p>
  
  <p>Node definition:</p>
  
  <pre><code>public class Node {
      public int value;
      public Node next;
      public Node(int data) {
          this.value = data;
      }
  }
  </code></pre>
  
  <p>Main code:</p>
  
  <pre><code>import java.util.*;
  
  public class RemoveLastKthNode_single {
      public static void main(String[] args) {
          int[] arr = {2, 4, 6, 8, 11, 3, 7};
          //Arrays.sort(arr);
  
          Node head = arrayToNode(arr);
  
          System.out.println("Initial LinkedList:");
          displayNode(head);
  
          System.out.println();
          int lastKth = 7;
          removeLastKthNode(head, lastKth);
          System.out.println("After remove last" + lastKth + "th" + " LinkedList:");
          displayNode(head);
      }
  
      // refer https://www.jianshu.com/p/0d0dbfcbc1c3
      public static Node arrayToNode(int[] arr) {
          Node head = new Node(arr[0]);
          Node other = head;
          for (int i = 1; i &lt; arr.length; i++) {
              Node temp = new Node(arr[i]);
              other.next = temp;
              other = other.next;
          }
          return head;
      }
  
      public static void displayNode(Node head) {
          while(head != null) {
              System.out.print(head.value + " ");
              head = head.next;
          }
          System.out.println();
      }
  
      public static Node removeLastKthNode(Node head, int lastKth) {
          if (head == null || lastKth &lt; 1) {
              return head;
          }
  
          Node cur = head;
          while (cur != null) {
              lastKth --;
              cur = cur.next;
          }
  
          if (lastKth == 0) {
              head = head.next;
          }
  
          if (lastKth &lt; 0) {
              cur = head;
              while (++lastKth != 0) {
                  cur = cur.next;
              }
              cur.next = cur.next.next;
          }
  
              System.out.println(head.value + " YES ");
          return head;
      }
  }
  </code></pre>
  
  <p>Thanks for Andreas's comment. I figure out how it works.</p>
  
  <p>Here is my logic. The reference to the "head node" is passed by value to "removeLastKthNode", so in memory I have a copy of head. And the copy of "head" also point to the "next node". It looks line a "two head snake". I can cut the "body" (because they are the exact one), but I cannot cut one snake "head" and make another "head" also be cut. Because they are different one in memory!</p>
  
  <p>Image here.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;p style="text-align:center;"&gt; &lt;img src="https://i.stack.imgur.com/3dfHw.png" width="280" height="150"&gt; &lt;/p&gt; </code></pre>
  </div>
  </div>
  </p>