21 Merge Two Sorted Lists

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.

First Attempt:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        // write your code here
        if (l1 == null) {
            return l2;
        } else if (l2 == null) {
            return l1;
        }
        // insert l2 in l1


        // deal with head first
        if (l2.val < l1.val) {
            ListNode insert = l2;
            l2 = l2.next;
            insert.next = l1;
            l1 = insert;
            if (l2 == null) {
                return l1;
            }
        }
        // deal with middle
        ListNode cur = l1;
        while (cur.next != null && l2 != null) {
            if (l2.val < cur.next.val) {
                ListNode insert = l2;   // insert here
                ListNode next = cur.next;
                l2 = l2.next;
                cur.next = insert;
                insert.next = next;
                cur = cur.next;
            } else {
                cur = cur.next;
            }
        }
        // deal with end
        if (l2 != null) {
            cur.next = l2;
        }
        return l1;
    }
}

Second attempt: use a dummy node and put the sequence in order.

public class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        // edge case

        ListNode lc1 = l1, lc2 = l2;
        // here: because the constructor is ListNode(int x) {val = x}, so you need to give an integer to the constructor
        ListNode dummy = new ListNode(0);
        ListNode cur = dummy;

        while (lc1 != null && lc2 != null) {
            if (lc1.val > lc2.val) {
                cur.next = lc2;
                lc2 = lc2.next;
                cur = cur.next;
            } else {
                cur.next = lc1;
                lc1 = lc1.next;
                cur = cur.next;
            }
        }

        if (lc1 != null) cur.next = lc1;
        if (lc2 != null) cur.next = lc2;
        return dummy.next;
    }
}

updated (12.18.15)

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null && l2 == null) {
            return null;
        }
        ListNode dummy = new ListNode(1);
        ListNode cur = dummy;
        while (l1 != null && l2 != null) {
            if (l1.val < l2.val) {
                cur.next = new ListNode(l1.val);
                l1 = l1.next;
            } else {
                cur.next = new ListNode(l2.val);
                l2 = l2.next;
            }
            cur = cur.next;
        }
        if (l1 != null) {
            cur.next = l1;
        }
        if (l2 != null) {
            cur.next = l2;
        }
        return dummy.next;
    }
}