Java Solution


  • 0
    P

    class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    ListNode dummy = new ListNode(-1);
    ListNode cursor = dummy;
    while(l1 != null && l2 != null) {
    if(l1.val <= l2.val) {
    cursor.next = l1;
    cursor = l1;
    l1 = l1.next;
    } else {
    cursor.next = l2;
    cursor = l2;
    l2 = l2.next;
    }
    }
    if(l1 == null) {
    cursor.next = l2;
    }
    if(l2 == null) {
    cursor.next = l1;
    }
    return dummy.next;
    }
    }


Log in to reply
 

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