Leetcode 21 合并两个有序链表

题目描述

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */

题解1(比较直接,参考归并排序最后的步骤)

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

提交结果

题解2(改进后两个循环,只改了写法,时空没变)

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode* dummy = new ListNode, *l3 = new ListNode;
        l3 = dummy;
        while(l1 && l2){
            if(l1->val <= l2->val){
                dummy->next = l1;
                l1 = l1->next;
            }else{
                dummy->next = l2;
                l2 = l2->next;
            }
            dummy = dummy->next;
        }
        //第一种解法太直球了,这个更舒服一点
        dummy->next = l1 ? l1 : l2; 
        return l3->next;
    }
};

提交结果

题解3(递归)

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        if (! l1) {
            return l2;
        } else if (! l2) {
            return l1;
        } else if (l1->val < l2->val) {
        //保留l1,查看是否需要修改l1->next
        //如果l1->val < l2->val l1=l1->next
        //相当于后面的工作是合并l1->next和l2
        //所以第一个参数改为l1->next
            l1->next = mergeTwoLists(l1->next, l2);
            return l1;
        } else {
        //同理
            l2->next = mergeTwoLists(l1, l2->next);
            return l2;
        }
    }
};

提交结果

题解4(直接迭代)

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
        // 判空
        if(!list1 && list2) return list2;
        if(!list2 && list1) return list1;
        // 头空结点
        ListNode* dummynode = new ListNode(0, 0);
        // 活动指针
        ListNode* h = dummynode;
        while(list1 || list2){
            int a{0}, b{0};

            if(list1)
                a = list1->val;
            else{
                h->next = list2;
                break;
            }

            if(list2)
                b = list2->val;
            else{
                h->next = list1;
                break;
            }

            if(a > b){
                h->next = new ListNode(b, 0);
                h = h->next;
                list2 = list2->next;
            }else if(a < b){
                h->next = new ListNode(a, 0);
                h = h->next;
                list1 = list1->next;
            }else{
                h->next = new ListNode(a, 0);
                h = h->next;
                h->next = new ListNode(b, 0);
                h = h->next;
                list1 = list1->next;
                list2 = list2->next;
            }  
        }
        return dummynode->next;
    }
};

在这里插入图片描述

改进
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
        ListNode* newlist = new ListNode(0);
        ListNode* tnewlist = newlist;
        while(list1 || list2){
            while(list1 && list2 && list1->val <= list2->val){
                newlist->next = new ListNode(list1->val);
                list1 = list1->next;
                newlist = newlist->next;
            }
            while(list1 && list2 && list2->val < list1->val){
                newlist->next = new ListNode(list2->val);
                list2 = list2->next;
                newlist = newlist->next;
            }
            if(! list1){
                newlist->next = list2;
                break;
            }
            if(! list2){
                newlist->next = list1;
                break;
            }
        }
        return tnewlist->next;
    }
};

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值