代码随想录第四天

24.两两交换链表中的结点

视频链接http://【把环形链表讲清楚! 如何判断环形链表?如何找到环形链表的入口? LeetCode:142.环形链表II】 https://www.bilibili.com/video/BV1if4y1d7ob/?share_source=copy_web&vd_source=a9e3519d86ff64b3c059efea3e631fc2

代码随想录的图片和讲解链接https://programmercarl.com/0024.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B9.html

cur要指向的是两两交换指针的前一个链表,在while循环的条件cur->next=NULL一定要写在前面,如果在第一个指针就为空的话把cur->next->next=NULL放前面会空指针异常。

题目链接https://leetcode.cn/problems/swap-nodes-in-pairs/description/

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode *dummyhead=new ListNode(0);
        dummyhead->next=head;
         ListNode *cur=dummyhead;
         while(cur->next!=NULL&&cur->next->next!=NULL){
            ListNode *tmp=cur->next;//保存1
            ListNode *tmp1=cur->next->next->next;//保存3
            cur->next=cur->next->next;//cur指向2
            cur->next->next=tmp;//2指向1
            tmp->next=tmp1;//1指向3
            cur=cur->next->next;//cur往后两位
         }
         return dummyhead->next;
    }
};

当cur指向2的时候cur原来那条线就断了无法访问到1和3了所以要先保存下来

19.删除链表中的倒数第N个节点

题目链接https://leetcode.cn/problems/remove-nth-node-from-end-of-list/description/

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* dummyhead=new ListNode(0);
        dummyhead->next=head;
        ListNode* fast=dummyhead;
        ListNode* slow=dummyhead;
        n++;
        while(n--&&fast!=NULL){
            fast=fast->next;
        }
        while(fast!=NULL){
             fast=fast->next;
             slow=slow->next;
        }
        ListNode* tmp=slow->next;
        slow->next=tmp->next;
        delete tmp;
        return dummyhead->next;
    }
};
哇,感觉这道题特别妙,要让快指针先走n+1步,这样让慢指针指向要删的那个结点的前一个,让慢指针前一个指向后一个把中间那个指针删掉就行了,就特别!!妙!!
while(n--&&fast->next!=NULL){
            fast=fast->next;
        }
最开始循环里面是这么写的就一直报错,循环里面是判断fast!=NULL,因为当fast指向空指针的时候fast->next就空指针异常了。

2.07链表相交

题目链接https://leetcode.cn/problems/intersection-of-two-linked-lists-lcci/description/

class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        ListNode* curA=headA;
        ListNode* curB=headB;
        int lenA,lenB;
        lenA=lenB=0;
        while(curA!=NULL){
            curA=curA->next;
            lenA++;
        }
        while(curB!=NULL){
            curB=curB->next;
            lenB++;
        }
         curA=headA;
         curB=headB;
        if(lenB>lenA){
            swap(lenB,lenA);
            swap(curB,curA);
        }
        
        int len=lenA-lenB;
        while(len--){
            curA=curA->next;
        }
        while(curA!=NULL){
            if(curA==curB){
                return curA;
            }
           curA=curA->next;
           curB=curB->next;
        }
        return NULL;
    }
};
在把curA和curB遍历完之后要记得把它赋值回去 然后再判断长度之后进行交换,因为要交换的是指向链表的头结点,所以要先赋值再进行交换

142.环形链表

题目链接https://leetcode.cn/problems/linked-list-cycle-ii/description/

慢指针在中间与快指针相遇

(x+y)*2=x+y+n(y+z) //慢指针的路程=快指针的路程

x+y=n(y+z)

x=n(y+z)-y

x=(n-1)(y+z)+z //当n=1的时候,x=z,当n!=1的时候,就是在相遇之后,快节点继续循环,与从头结点来的指针在环形入口点相遇。

class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode* fast=head;
        ListNode* slow=head;
        while(fast!=NULL&&fast->next!=NULL){//确保Fast下一个不是空指针以免
            fast=fast->next->next;          //fast->next->next报错
            slow=slow->next;
            if(fast==slow){
                ListNode* index1=fast;
                ListNode* index2=head;
                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、付费专栏及课程。

余额充值