title: How to add elements in a Linked list by recursion?
body: <p>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</p>
  
  <pre><code>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;
  }
  }
  </code></pre>