2

I have made a private recursive method called "add" that should recursively add elements but it is not working. I know that java doesn't have pass by reference, so how would one add elements recursively? It would be great if you could tell me where I am wrong. Thanks

public class linkedIt2 {
private int length = 0;
private Node head;


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

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


public linkedIt2() {
    head = null;
}

private void add(Node cur, int data) {
    if (cur != null) {
        add(cur.next, data);
    } else {
        cur = new Node(data, null);
    }
}

public linkedIt2 insert(int data) {
    add(this.head, data);
    length++;
    return this;
}
}
1
  • 1
    What does "not work" mean? Commented Jul 27, 2017 at 19:43

2 Answers 2

2

The problem is, you can't change the reference. The cur.next never gets changed because the references are called by value. You need to change cur.next before the recursion call.

You need to understand the java concept of "call by value" there is no call by reference. The references are transfered as value, so change the reference-parameter itself, nothing happens to the originally used object. perhaps: http://javadude.com/articles/passbyvalue.htm makes it clearer.

One solution: similar to your's:

Initially you have to make sure that cur is not null. then you can access cur.next inside the function. if it is null, end the recursion, if not then go deeper.

private void add(Node cur, int data) {
  if (cur.next != null) {
    add(cur.next, data);      
  } else {
    cur.next = new Node(data, null);
  }
}

public linkedIt2 insert(int data) {
  if (this.head == null) {
     this.head = new Node(data, null);
  } else {
     add(this.head, data);
  }
  length++;
  return this;
}

or let Node do the recursion and manipulate itself. As the other solution states. That's even nicer.

Sign up to request clarification or add additional context in comments.

3 Comments

But how do I change cur.next before the recursive call if I cannot refer to it?
Thank you so much. One question, how come when I pass cur.next, it works but when I pass cur it doesn't? Aren't both being called by value?
You can manipulate next only, if you have the reference to the object cur available. The passed reference itself you can not change, what is pointed at by the reference, you can change. That would not be the case, if the object in the call was passed by value (as in C or C++). Since you would only change a copy. That is in Java not possible. Objects are always passed as references.
0

Your mistake here is in the line

cur = new Node(data, null);

When you call this it updates the local variable cur to point to a new node, which doesn't actually cause the variable you called the method with to update. To give a simpler example

public static void setToFive(int num) {
    num = 5;
    System.out.println(num); //Always prints 5
}

public static void test() {
    int test = 3;
    setToFive(test);
    System.out.println(test); //Still prints 3 since only num was updated, not test
}

So to return to the original problem and add elements recursively you could do something like

private class Node {
    //...
    public void add(int data) {
        if (next == null) {
            next = new Node(data, null);
        } else {
            next.add(data);
        }
}

then simply call head.add(data) and increment your length counter.

This version works better since when it creates the new node it sets the instance variable of the class to point to it rather than a variable contained within the method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.