代码随想录算法训练营第四天|链表 24. 两两交换 19.删除倒数第n个 160.链表相交 142.环形链表

24. 两两交换

此题小心while循环里面, 判断条件,小心引用空指针,找好边界

1. ->next (=null)访问到空指针没问题,但是 ->next->next(null->next) 访问空指针的next就不行了,会出现 member access within null pointer的问题

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode *dummyHead=new ListNode(-1);
        dummyHead->next=head;
        ListNode *cur=dummyHead;
        ListNode *temp1;
        ListNode *temp2;
        //当cur指到3的时候就要停了
        while(cur->next!=nullptr&& cur->next->next!=nullptr)
        {
            temp1=cur->next;
            cur->next=temp1->next;
            temp2=cur->next->next;//保存3的地址
            //这个地方取null是没错的,但是null的next就是空指针的访问了,是循环条件的问题
            //temp2=(cur->next->next==NULL? NULL:cur->next->next);  //把3的地址保存住
            cur->next->next=temp1;
            temp1->next=temp2;
            cur=cur->next->next;   //把cur往后挪两位
        }
        head=dummyHead->next;
        delete dummyHead;
        return head;
    }
};

19.删除倒数第n个

傻子选择直接两次遍历

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        //使用两趟扫描,先确定长度
        //依旧使用虚拟头结点
        int size=0;
        ListNode *dummyHead=new ListNode(-1);
        dummyHead->next=head;
        ListNode *p=head;
        while(p)
        {
            p=p->next;
            size++;
        }//p在这里又变成null了
        p=dummyHead;
        int t=size-n;
        while(t)
        {
            p=p->next;
            t--;
        }
        ListNode *temp=p->next;
        p->next=temp->next;
        delete temp;
        ListNode *res=dummyHead->next;
        delete dummyHead;
        return res;
    }
};

复杂一点的双指针方法,学习思路

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        //使用快慢指针
        //使用虚拟头结点
        //找到思路,简直一遍过,呜呜呜
        ListNode *dummyHead=new ListNode(-1);
        dummyHead->next=head;
        ListNode *lowindex=dummyHead;
        ListNode *fastindex=dummyHead;
        while(n+1)//把快指针挪到对应的位置
        {
            fastindex=fastindex->next;
            n--;
        }
        while(fastindex) //快指针现在就指在合适的位置
        {
            lowindex=lowindex->next;
            fastindex=fastindex->next;
        }
        ListNode *temp=lowindex->next;
        lowindex->next=lowindex->next->next;
        delete temp;
        ListNode *res= new ListNode();
        res=dummyHead->next;
        delete dummyHead;
        return res;
    }
};

160.链表相交

比较简单

142.环形链表

1. 明天继续看一下  为什么n=1 ,n至少为1圈

class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        //快慢指针会相遇则代表有环
        //计算出在环里相遇的位置,x=z
        ListNode *lowindex=head;
        ListNode *fastindex=head;
        while(fastindex!=NULL&&fastindex->next!=NULL)
        {
            fastindex=fastindex->next->next;
            lowindex=lowindex->next;
            if(fastindex==lowindex)
            {
                ListNode *index1= head;
                ListNode *index2= fastindex;
                while(index1!=index2)
                {
                    index1=index1->next;
                    index2=index2->next;
                }
                return index1;
            }
        }

        return NULL;
        
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值