use dummy node to implement easily with JAVA


  • 0
    R

    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    if(l1==null && l2==null) return null;
    if(l1==null) return l2;
    if(l2==null) return l1;
    ListNode dummy = new ListNode(0);
    ListNode pre = dummy;
    while(l1 != null && l2 != null) {
    if(l1.val > l2.val) {
    pre.next = l2;
    l2 = l2.next;
    } else {
    pre.next = l1;
    l1 = l1.next;
    }
    pre = pre.next;
    }
    if(l1 != null) { pre.next = l1; }
    if(l2 != null) { pre.next = l2; }
    return dummy.next;
    }


Log in to reply
 

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