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;

    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值