LeetCode21合并二个排序链表

/**
 * 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* first=l1;
        ListNode* second=l2;
        ListNode* cur;
        ListNode* pHead;
        if(first==nullptr) return second;
        if(second==nullptr) return first;
        if(first->next>second->next){
            cur=second;
            pHead=second;
            second++;              //傻逼错误
        }
        if(first->next<=second->next){
            cur=first;
            pHead=first;
            first++;               //傻逼错误 
        }
        while(first->next==nullptr||second->next==nullptr){   //条件写错
            if(first->val>second->val){
                cur->next=second;
                cur=cur->next;
                second++;              //傻逼错误
            }else if(first->val<=second->val){
                cur->next=first;
                cur=cur->next;
                first++;              //傻逼错误
            }
        }
        if(first) cur->next=first;
        if(second) cur->next=second;
        return pHead;
    }
};
first->next==nullptr||second->next==nullptr

先是把||改成&&,然后又发现first指向空后,first->next报出异常
条件是first和second均不为空,first和second的变化规律是每当·first和second中的较小节点被加入结果链表中是,first和second就往后移一位,当某一个链表中的节点都被加入结果链表中时,first和second必有一个指向空。
测试用例
[-10,-10,-9,-4,1,6,6]
[-7]
不通过

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode* first=l1;
        ListNode* second=l2;
        ListNode* cur;
        ListNode* pHead;
        if(first==nullptr) return second;
        if(second==nullptr) return first;
        if(first->val>second->val){
            cur=second;
            pHead=second;
            second=second->next;
        }
        if(first->val<=second->val){
            cur=first;
            pHead=first;
            first=first->next;
        }
        while(first&&second){   //
            if(first->val>second->val){
                cur->next=second;
                cur=cur->next;
                second=second->next;
            }else if(first->val<=second->val){
                cur->next=first;
                cur=cur->next;
                first=first->next;
            }
        }
        if(first) cur->next=first;
        if(second) cur->next=second;
        return pHead;
    }
};

通过上面测试用例,但
[2]
[1]
不能通过,报Line 15: member access within null pointer of type ‘struct ListNode’ first为空first->val<=second->val报出异常,把二个并行的if改成if…else if就不会出现这样的错误,上次写删除双向链表中的某个节点也是这样的错误用二个并行的if,导致报出异常
觉悟:某些情况下只能用if…else if ,if…else if比二个并行的if要好,用二个并行的if会出错(抛出异常)

改掉后:

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode* first=l1;
        ListNode* second=l2;
        ListNode* cur;
        ListNode* pHead;
        if(first==nullptr) return second;
        if(second==nullptr) return first;
        if(first->val>second->val){
            cur=second;
            pHead=second;
            second=second->next;
        }else if(first->val<=second->val){    ///
            cur=first;
            pHead=first;
            first=first->next;
        }
        while(first&&second){
            if(first->val>second->val){
                cur->next=second;
                cur=cur->next;
                second=second->next;
            }else if(first->val<=second->val){
                cur->next=first;
                cur=cur->next;
                first=first->next;
            }
        }
        if(first) cur->next=first;
        if(second) cur->next=second;
        return pHead;
    }
};

在这里插入图片描述
这里面对代码做一些简化:
cur的初始化是通过比较二个链表的头结点的大小来决定cur应该指向那个节点
这里面引入一个辅助节点,简化代码编写,最后返回辅助节点的next

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode* first=l1;
        ListNode* second=l2;
        ListNode* cur;
        if(first==nullptr) return second;
        if(second==nullptr) return first;
        ListNode helperNode(0);
        cur=&helperNode;
        while(first&&second){
            if(first->val>second->val){
                cur->next=second;
                cur=cur->next;
                second=second->next;
            }else if(first->val<=second->val){
                cur->next=first;
                cur=cur->next;
                first=first->next;
            }
        }
        if(first) cur->next=first;
        if(second) cur->next=second;
        return helperNode.next;
    }
};

参考文献:https://blog.csdn.net/obrcnh/article/details/78877070

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值