Merge Two Sorted Lists by splicing together the nodes of the first two lists I solved this problem:


  Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.


How can I improve its execution time and also, if there is any better way of doing it?

public ListNode mergeTwoLists(ListNode listNode1, ListNode listNode2) {
    ListNode headNode = new ListNode(0);
    ListNode node = headNode;
    while (null != listNode1 &amp;&amp; null != listNode2) {
        if (listNode1.val &lt; listNode2.val) {
            node.next = listNode1;
            listNode1 = listNode1.next;
        } else {
            node.next = listNode2;
            listNode2 = listNode2.next;
        }
        node = node.next;
    }
    if(null != listNode1) {
        node.next = listNode1;
    }
    if(null != listNode2) {
        node.next = listNode2;
    }
    return headNode.next;
}
LGTM.

Still a couple of notes:


A null in a conditional context evaluates to false. It is safe to omit an explicit comparison to null, along the lines of

    while (listNode1 &amp;&amp; listNode2) {


Ditto for if (listNode1) and if (listNode2).
Mandatory stability loss notice. The if (listNode1.val &lt; listNode2.val) test loses stability: when the values compare equal, an element from the second list is merged first.

The stability of merge is not required by the problem statement, and doesn't matter for integers. Just be aware.