My ac Java solution


  • 15
    V
     public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
            ListNode returnNode = new ListNode(-1);
            ListNode headNode = returnNode;
            while (l1 != null && l2 != null) {
                if (l1.val <= l2.val) {
                    returnNode.next = l1;
                    l1 = l1.next;
                } else {
                    returnNode.next = l2;
                    l2 = l2.next;
                }
                returnNode = returnNode.next;
            }
            if (l1 == null) {
                returnNode.next = l2;
            } else if (l2 == null) {
                returnNode.next = l1;
            }
            return headNode.next;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.