1

I am practicing using Java's LinkedList.
The purpose of the main method here is to remove the given index's node.

The given node can be removed rightly, except the first node.

If I changed
removeLastKthNode(head, lastKth); into
head = removeLastKthNode(head, lastKth), it works.

But I don't know why removeLastKthNode(head, lastKth) cannot remove the first node.

Andreas gave me a link about this problem Is Java "pass-by-reference" or "pass-by-value"?

However, problem here is that "removeLastKthNode" behaves as "pass by reference" when "lastKth" doesn't refer the first node(head node).

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" ?

It is confusing here.

Some results below:
when "lastKth = 6"

Initial LinkedList:
2 4 6 8 11 3 7

After remove last6th LinkedList:
2 6 8 11 3 7

when "lastKth = 7"

Initial LinkedList:
2 4 6 8 11 3 7

After remove last7th LinkedList:
2 4 6 8 11 3 7

Thanks for sekky's advice. I wrote the code myself and add to the code below.

Node definition:

public class Node {
    public int value;
    public Node next;
    public Node(int data) {
        this.value = data;
    }
}

Main 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 < 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 < 1) {
            return head;
        }

        Node cur = head;
        while (cur != null) {
            lastKth --;
            cur = cur.next;
        }

        if (lastKth == 0) {
            head = head.next;
        }

        if (lastKth < 0) {
            cur = head;
            while (++lastKth != 0) {
                cur = cur.next;
            }
            cur.next = cur.next.next;
        }

            System.out.println(head.value + " YES ");
        return head;
    }
}

Thanks for Andreas's comment. I figure out how it works.

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!

Image here.

<p style="text-align:center;"> <img src="https://i.stack.imgur.com/3dfHw.png" width="280" height="150"> </p> 
tao4free
  • 13
  • 5
  • Welcome to stackoverflow! Is the Node class you are using one you wrote yourself? If so, can you provide it? – sekky Apr 20 '19 at 13:33
  • Thank you for comment. Yes, I wrote the Node class. Here it is. I have add them to the code. Thank you. – tao4free Apr 20 '19 at 23:25
  • Possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – tao4free Apr 21 '19 at 10:27

0 Answers0