title: How to append a Linked List to back of another?
body: <p>I've implemented my own Linked List type of data structure but I'm running into a problem when I want to append one linked list to another without iterating over anything. </p>
  
  <p>Here is an example with the output I wanted</p>
  
  <pre><code>public class MyList{
  
      public static void main(String[] args){
          MyList list1 = new MyList(3);
          MyList list2 = new MyList(4);
  
          System.out.println(list1);  //0 1 2
          System.out.println(list2);  //0 1 2 3
  
          list1.add(list2);
  
          System.out.println(list1);  //0 1 2 0 1 2 3
          System.out.println(list2);  //0 1 2 3
      }
  
      private class Node{
          public int data;
          public Node next;
  
          public Node(int data){
              this.data = data;
          }
      }
  
      public Node head;
      public Node tail;
  
      public MyList(int length){
          for(int i = 0; i &lt; length; i++){
              add(new Node(i));
          }
      }
  
      public void add(Node node) {
          if (head == null) {
              //insert first node
              head = node;
              tail = node;
          } else {
              //add node to end
              tail.next = node;
              tail = tail.next;
          }
      }
  
      //Problem!
      public void add(MyList list) {
  
      } 
  
      @Override
      public String toString(){
          String result = "";
          for(Node iter = head; iter != null; iter = iter.next){
              result += iter.data + " ";
          }
          return result;
      }   
  }
  </code></pre>
  
  <p>When list2 is added to list1, I want list1 to be extended without breaking the original list2. I can't see how to do this without iterating over anything though. It is trivial to iterate over list2 in the add method and add each node to the end individually but this doesn't feel right to do with linked lists.</p>
  
  <p>Can anyone give me some advice on how I could do this efficiently</p>