Merge Two Linked Lists using Java
<p>Trying to figure out what I'm missing in my code that is supposed to merge linked list 2 to the end of linked list 1.  Right now it's just getting the last element in the second list and returning that.</p>

<p>The logic I was trying to use is walking down the first list (L1) and adding those elements one-by-one to new_list then doing the same for the second list (L2) after I've reached the end of L1.  I am also trying to avoid modifying L1 or L2, which is why I created a new_list.</p>

<p>Any help would be greatly appreciated.</p>

<pre><code>public NodeList(int item, NodeList next) {
    this.item = item;
    this.next = next;
}

public static NodeList merge(NodeList l1, NodeList l2) {

    NodeList new_list = new NodeList(l1.item, l1.next);
    NodeList new_list2 = new NodeList(l2.item, l2.next);

    while (true) {
        if (new_list.next == null) {
            if (new_list2.next == null) {
                return new_list;
            }
            else {
                new_list.next = new NodeList(new_list2.next.item, new_list2.next.next);
                new_list2 = new_list2.next;
            }

        }
        else {
            new_list.next = new NodeList(new_list.next.item, new_list.next.next);
            new_list = new_list.next;
        }
    }
}
</code></pre>