Leetcode - Merge Two Sorted Lists

Leetcode - Merge Two Sorted Lists_第1张图片

My code:

/**
 * 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 l2;
        else if (l1 != null && l2 == null)
            return l1;
        else if (l1 == null && l2 == null)
            return null;
        ListNode temp1 = l1;
        ListNode temp2 = l2;
        ListNode head = null;
        
        if (l1.val <= l2.val) {
            head = l1;
            temp1 = l1.next;
        }
        else {
            head = l2;
            temp2 = l2.next;
        }
        ListNode temp = head;
        ListNode tail = temp;
        while (temp1 != null && temp2 != null) {
            if (temp1.val <= temp2.val) {
                temp.next = temp1;
                temp = temp.next;
                if (temp1.next == null)
                    tail = temp1;
                temp1 = temp1.next;
            }
            else {
                temp.next = temp2;
                temp = temp.next;
                if (temp2.next == null)
                    tail = temp2;
                temp2 = temp2.next;
            }
        }
        if (temp1 == null)
            tail.next = temp2;
        else if (temp2 == null)
            tail.next = temp1;
        return head;
    }
}

My test result:

Leetcode - Merge Two Sorted Lists_第2张图片
Paste_Image.png

和之前的差不多做法,还是比较简单的,除了要处理一些细节问题。

补:只不过可以看出一个明显的问题,头结点需要用if语句来确定,逻辑要麻烦一点。然后网上无意间看到了一个做法,叫做 Dummy Node, 很好,分享如下。

private ListNode merge(ListNode head1, ListNode head2) {
         ListNode dummy = new ListNode(-1);
         ListNode head = dummy;
         while (head1 != null && head2 != null ) {
            if (head1.val < head2.val) {
                head.next = head1;
                head1 = head1.next;
            }else {
                head.next = head2;
                head2 = head2.next;
            }
            head = head.next;
            
        }
         if (head1 != null) {
                    head.next = head1;
          }
         else {
                head.next = head2;
          }
        return dummy.next;
    }
Leetcode - Merge Two Sorted Lists_第3张图片
Paste_Image.png

**
总结: Array
**

Anyway, Good luck, Richardo!

My code:

/**
 * 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)
            return l2;
        else if (l2 == null)
            return l1;
        ListNode dummy = new ListNode(-1);
        ListNode temp = dummy;
        while (l1 != null && l2 != null) {
            if (l1.val < l2.val) {
                temp.next = l1;
                l1 = l1.next;
            }
            else {
                temp.next = l2;
                l2 = l2.next;
            }
            temp = temp.next;
        }
        temp.next = (l1 == null) ? l2 : l1;
        return dummy.next;
    }
}

没什么难的。

Anyway, Good luck, Richardo!

差不多的做法。。
想当年, dummy node也不知道啊。
只不过知道了也没多大用。

Anyway, Good luck, Richardo! -- 08/15/2016

你可能感兴趣的:(Leetcode - Merge Two Sorted Lists)