leetcode_[python/C++]_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.
【分析】
做法(1):可以在new_list前先加一个无关的节点然后返回new_list->next
做法(2):正常做法比较,合并
做法(3):递归
做法(4):事先假定(最高效)
最优runtime:
这里写图片描述
这里写图片描述


C++:
正常做法:

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

         }  
          else                         
         {   
             new_list = l2;   
             l2 = l2->next;  
        } 
        ListNode * pre = new_list;
        while( l1 != NULL  && l2 != NULL ){
            if( l1->val < l2->val ){
                pre->next = l1;
                l1 = l1->next;
            }
            else{
                pre->next = l2;
                l2 = l2->next;
            }
            pre = pre->next;
        }

        if( l1 ) pre->next = l1;
        else pre->next = l2;
        return new_list;

    }
};

增加一个无关节点:

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        if(!l1 && !l2) return NULL;
        ListNode * new_list = new ListNode(0);
        ListNode * pre = new_list;
        while( l1 != NULL  && l2 != NULL ){
            if( l1->val < l2->val ){
                pre->next = l1;
                l1 = l1->next;
            }
            else{
                pre->next = l2;
                l2 = l2->next;
            }
            pre = pre->next;
        }
        if( l1 )  pre->next = l1;
        else  pre->next = l2;
        return new_list->next;

    }
};

递归:

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(l2->next, l1);  
            return l2;  
        }  
    }  
};  

事先假定:

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode * new_list = new ListNode(0);
        new_list->next = l1;
        ListNode * prev = new_list;
        ListNode * headof1 = l1;
        ListNode * headof2 = l2;
        while( headof2 != NULL ){
            if( headof1 == NULL ){
                prev->next = headof2;
                break;
            }
            if( headof1->val > headof2->val ){
                ListNode * temp = headof2;
                headof2 = headof2->next;
                prev->next = temp;
                temp->next = headof1;
                prev = prev->next;
            }
            else{

                prev = headof1;
                headof1 = headof1->next;
            }
        }
        return new_list->next;
    }
};

python:
递归:

def mergeTwoLists(self, l1, l2):  
    if not l1:  
        return l2  
    elif not l2:  
        return l1  
    else:  
        if l1.val <= l2.val:  
            l1.next = self.mergeTwoLists(l1.next, l2)  
            return l1  
        else:  
            l2.next = self.mergeTwoLists(l1, l2.next)  
            return l2 

非递归:

class Solution(object):
    def mergeTwoLists(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        if l1 is None and l2 is None:
            return None
        new_list = ListNode(0)
        pre = new_list
        while l1 is not None and l2 is not None:
            if l1.val < l2.val:
                pre.next = l1
                l1 = l1.next
            else:
                pre.next = l2
                l2 = l2.next
            pre = pre.next
        if l1 is not None:
            pre.next = l1
        else:
            pre.next = l2
        return new_list.next

事先假定:

class Solution(object):
    def mergeTwoLists(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        new_list = ListNode(0)
        new_list.next = l1
        prev, head_of_1, head_of_2 = new_list, l1, l2

        while head_of_2:
            if not head_of_1:
                prev.next = head_of_2
                break
            if head_of_1.val > head_of_2.val:
                temp = head_of_2
                head_of_2 = head_of_2.next
                prev.next = temp
                temp.next = head_of_1
                prev = prev.next
            else:
                head_of_1, prev = head_of_1.next, head_of_1
        return new_list.next
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值