代码随想录算法训练营第4天

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

在这里插入图片描述

  • 一开始想的是交换两个结点,比如12交换,再交换34,然后发现1无法指向4,所以正确的做法是操作三个结点,也引出虚拟头结点。
  • 三步骤:(记不住)看图清晰点
    在这里插入图片描述

当前结点指向2,2指向1,1指向3

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode* dummyhead=new ListNode(0);
        dummyhead->next=head;
        ListNode* tmp,*tmp1;//这样定义tmp1前要加*
        ListNode* cur=dummyhead;
        while(cur->next!=nullptr&&cur->next->next!=nullptr){
            tmp=cur->next;
            tmp1=cur->next->next->next;//易错
            cur->next=cur->next->next;
            cur->next->next=tmp;
            cur->next->next->next=tmp1;//易错

            cur=cur->next->next;
        }
        return dummyhead->next;
    }
};

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

  • 双指针,思想fast比slow快n步,然后同时移动,fast走到最后的时候,slow就是倒数第n个
  • 注意细节,要删除倒数第n个,其实是要找到倒数第n-1个
lass Solution {//双指针法
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* dummyhead=new ListNode(0);
        dummyhead->next=head;
        ListNode* slow=dummyhead,*fast=dummyhead;
        for(int i=0;i<n;i++){
            fast=fast->next;
        }
        fast=fast->next;//多走一步,为了让slow指向要删除的前一个结点
        while(fast!=nullptr){
            slow=slow->next;
            fast=fast->next;
        }
        slow->next=slow->next->next;
        return dummyhead->next;
    }
};

面试题 02.07. 链表相交

  • 关键是:两个链表末尾位置对齐,从那个起始点开始移动比较
  • 链表相交不是val值相同,而是指针相等
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        ListNode* cura=headA,*curb=headB;
        int lena=0,lenb=0;
        while(cura!=nullptr){
            lena++;
            cura=cura->next;
        }
        while(curb!=nullptr){
            lenb++;
            curb=curb->next;
        }
        cura=headA,curb=headB;
        if(lenb>lena){
            swap(lena,lenb);
            swap(cura,curb);
        }
        int diff=lena-lenb;
        for(int i=0;i<diff;i++)
            cura=cura->next;
        while(cura!=nullptr&&curb!=nullptr){
            if(cura==curb)
                return cura;
            else {
                cura=cura->next;
                curb=curb->next;
            }
        }
        return NULL;
    }
};

142. 环形链表 II

  • 判断是不是环,快慢指针,快每次走两步,慢每次走一步,看是否可相遇,是则为环
  • 判断环的入口,一个从相遇的地方出发,一个从头结点出发,每次都走一步,相遇的地方是入口。(证明看代码随想录环形链表II
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode* slow=head,*fast=head;
        while(fast!=NULL&&fast->next!=NULL){//原因:因为fast要走两步
            slow=slow->next;
            fast=fast->next->next;
            if(slow==fast){
                ListNode* tmp=slow;
                ListNode* tmp1=head;
                while(tmp!=tmp1){
                    tmp=tmp->next;
                    tmp1=tmp1->next;
                }
                return tmp;
            }
        }
        return NULL;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值