1. 程式人生 > >Leetcode_Linked_List --21. Merge Two Sorted Lists [easy]

Leetcode_Linked_List --21. Merge Two Sorted Lists [easy]

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. 將兩個有序連結串列合併為一個新連結串列,這個新連結串列應該是由第一個連結串列和第二個連結串列拼接而成

Example:

Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4

Solution:

Python


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

#原址迭代
class Solution:
    def mergeTwoLists(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        if None in (l1,l2):   #這一步是必須的,只有先保證l1和l2都存在,才能執行while迴圈
            return l1 or l2
        dummy = cur = ListNode(0)   #建立輔助頭結點
        dummy.next = l1
        while l1 and l2:
            if  l1.val < l2.val:
                l1 = l1.next
            else:
                suc = cur.next   #先將要插入的節點位置後面的連結串列儲存
                cur.next = l2   #在cur中插入l2所指的節點
                tmp = l2.next   #先將l2後面的連結串列儲存
                l2.next = suc   #將cur上的l2節點與插入位置後面的原連結串列相連線,插入完成
                l2 = tmp   #相當於更新l2的頭結點,指向l2的下一個位置
            cur = cur.next   #更新cur頭結點,指向下一個位置
        cur.next = l1 or l2   #將多餘的剩下連結串列接到cur上
        return dummy.next

#迭代方法
class Solution:
    def mergeTwoLists(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        dummy = cur = ListNode(0)   #非原址,需要建立一個輔助連結串列cur
        while l1 and 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 or l2
        return dummy.next

#遞迴方法
class Solution:
    def mergeTwoLists(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        if None in (l1,l2):
            return l1 or l2
        if l1.val < l2.val:
            l1.next = self.mergeTwoLists(l1.next,l2)
            return l1
        else:
            l2.next = self.mergeTwoLists(l1,l2.next)
            return l2

原址的方法沒有開闢新空間,只是多了兩個指標dummy和cur,用的是連結串列插入的元素的方法。迭代方法中的cur是一個新的連結串列,開闢了新空間

C++的思路與Python一樣

C++

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode *dummy = new ListNode(-1);
        ListNode *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;

    }
};