2
\$\begingroup\$

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 && null != listNode2) {
        if (listNode1.val < 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;
}
\$\endgroup\$
3
\$\begingroup\$

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 && listNode2) {
    

    Ditto for if (listNode1) and if (listNode2).

  • Mandatory stability loss notice. The if (listNode1.val < 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.

\$\endgroup\$
  • 1
    \$\begingroup\$ A null in a conditional context evaluates to false This is not correct. It's a syntax error in Java to not have an explicit test. \$\endgroup\$ – markspace Sep 26 at 13:56
1
\$\begingroup\$

Scenario will fail if first value is negative and if one list is empty then it wil give error Below is my working code

public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode first;
        ListNode second;
        if(l1!=null && l2 !=null) {
            if(l1.val<=l2.val) {
                first = l1;
                l1 =l1.next;
            } else {
                first = l2;
                l2 =l2.next;
            }
            second = first;
            while(l1!=null && l2!=null) {
                if(l1.val<=l2.val) {
                    first.next = l1;
                    l1 = l1.next;

                } else {
                    first.next = l2;
                    l2 = l2.next;
                }
                first = first.next;
            }
            if(null != l1) {
                first.next = l1;
            }
            if(null != l2) {
                first.next = l2;
            }
            return second;
        } else {
            if(l2 == null) {
                return l1;
            } else if(l1==null) {
                return l2;
            } else {
                return l1;
            }
        }
    }
```
\$\endgroup\$
  • 2
    \$\begingroup\$ When providing an alternative offer, it's expected you also review the original code. Could you edit your answer? \$\endgroup\$ – dfhwze Sep 26 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.