[LeetCode] Merge Two Sorted Lists 混合插入有序連結串列

Grandyang發表於2014-11-10

 

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.

 

這道混合插入有序連結串列和我之前那篇混合插入有序陣列非常的相似Merge Sorted Array,僅僅是資料結構由陣列換成了連結串列而已,程式碼寫起來反而更簡潔。具體思想就是新建一個連結串列,然後比較兩個連結串列中的元素值,把較小的那個鏈到新連結串列中,由於兩個輸入連結串列的長度可能不同,所以最終會有一個連結串列先完成插入所有元素,則直接另一個未完成的連結串列直接鏈入新連結串列的末尾。程式碼如下:

 

C++ 解法一:

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode *dummy = new ListNode(-1), *cur = dummy;
        while (l1 && l2) {
            if (l1->val < l2->val) {
                cur->next = l1;
                l1 = l1->next;
            } else {
                cur->next = l2;
                l2 = l2->next;
            }
            cur = cur->next;
        }
        cur->next = l1 ? l1 : l2;
        return dummy->next;
    }
};

 

Java 解法一:

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

 

下面我們來看遞迴的寫法,當某個連結串列為空了,就返回另一個。然後核心還是比較當前兩個節點值大小,如果l1的小,那麼對於l1的下一個節點和l2呼叫遞迴函式,將返回值賦值給l1.next,然後返回l1;否則就對於l2的下一個節點和l1呼叫遞迴函式,將返回值賦值給l2.next,然後返回l2,參見程式碼如下:

 

C++ 解法二:

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        if (!l1) return l2;
        if (!l2) return l1;
        if (l1->val < l2->val) {
            l1->next = mergeTwoLists(l1->next, l2);
            return l1;
        } else {
            l2->next = mergeTwoLists(l1, l2->next);
            return l2;
        }
    }
};

 

Java 解法二:

public class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null) return l2;
        if (l2 == null) return l1;
        if (l1.val < l2.val) {
            l1.next = mergeTwoLists(l1.next, l2);
            return l1;
        } else {
            l2.next = mergeTwoLists(l1, l2.next);
            return l2;
        }
    }
}

 

下面這種遞迴的寫法去掉了if從句,看起來更加簡潔一些,但是思路並沒有什麼不同:

 

C++ 解法三:

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        if (!l1) return l2;
        if (!l2) return l1;
        ListNode *head = l1->val < l2->val ? l1 : l2;
        ListNode *nonhead = l1->val < l2->val ? l2 : l1;
        head->next = mergeTwoLists(head->next, nonhead);
        return head;
    }
};

 

Java 解法三:

public class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null) return l2;
        if (l2 == null) return l1;
        ListNode head = (l1.val < l2.val) ? l1 : l2;
        ListNode nonhead = (l1.val < l2.val) ? l2 : l1;
        head.next = mergeTwoLists(head.next, nonhead);
        return head;
    }
}

 

 我們還可以三行搞定,簡直喪心病狂有木有!

 

C++ 解法四:

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        if (!l1 || (l2 && l1->val > l2->val)) swap(l1, l2);
        if (l1) l1->next = mergeTwoLists(l1->next, l2);
        return l1;
    }
};

 

Java 解法四:

public class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null || (l2 != null && l1.val > l2.val)) {
            ListNode t = l1; l1 = l2; l2 = t;
        }
        if (l1 != null) l1.next = mergeTwoLists(l1.next, l2);
        return l1;
    }
}

 

類似題目:

Merge Sorted Array

 

參考資料:

https://discuss.leetcode.com/topic/2513/a-recursive-solution

https://discuss.leetcode.com/topic/18709/3-lines-c-12ms-and-c-4ms/2

https://discuss.leetcode.com/topic/32953/java-recursive-solution-in-6-lines

 

LeetCode All in One 題目講解彙總(持續更新中...)

相關文章