Add All to Tail
by Isai Damier, Android Engineer @ Google

/***************************************************************************
 * Author: Isai Damier
 * Title: Singly Linked List
 * Project: geekviewpoint
 * Package: datastructure
 *
 * Description: A LinkedList is a data structure that allows access
 *   to a collection of data using pointers/references. While an
 *   array can also be defined as above, LinkedLists and arrays differ
 *   in how they are stored in memory and in the operations they
 *   allow. Unlike an array that must be stored in a block of memory,
 *   the nodes of a LinkedList can be stored anywhere because each
 *   node has a reference to the node that succeeds it. Because the
 *   nodes are stored so loosely, inserting nodes into a LinkedList
 *   is easy; whereas in an array, all the succeeding elements must
 *   be shifted. Of course, insertion also means changing the size of
 *   the array, which means creating the entire array anew.
 *
 *   Perhaps the greatest beauty of LinkedList is that it allows
 *   accessing an entire sequence of nodes using only one variable:
 *   a reference to the first node in the sequence.
 *
 *   Countless operations can be performed on LinkedLists. Following
 *   are a few, ranging from the common to the very interesting.
 **************************************************************************/ 
 public class SinglyLinkedList {

  Node head = null;
  Node tail = null;

  /*****************************************************************
   * Time Complexity of Solution:
   *   O(n).
   *
   * Description: Add all the nodes of the given list to this linked list.
   *****************************************************************/
  public void addAllToTail(Node subHead) {
    if (null == subHead) {
      return;
    }
    if (this.isEmpty()) {
      this.head = subHead;
    } else {
      tail.next = subHead;
    }
    for (; null != subHead; subHead = subHead.next);
    this.tail = subHead;
  }
}
public class SinglyLinkedListTest {

  /**
   * Test of addAllToTail method, of class SinglyLinkedList.
   */
  @Test
  public void testAddAllToTail() {
    System.out.println("addAllToTail");
    int[] in_1 = {29, 14, 35, 2, 1, 12};
    int[] in_2 = {99, 78, 8, 3, 23};

    SinglyLinkedList list_1 = new SinglyLinkedList();
    SinglyLinkedList list_2 = new SinglyLinkedList();

    for (int i = 0; i < in_1.length; i++) {
      list_1.addToTail(in_1[i]);
    }
    assertEquals(in_1.length, list_1.size());

    for (int i = 0; i < in_2.length; i++) {
      list_2.addToTail(in_2[i]);
    }
    assertEquals(in_2.length, list_2.size());

    list_2.addAllToTail(list_1.head);
    assertEquals(in_1.length + in_2.length, list_2.size());
  }
}