代码随想录第四天

本文介绍了四种关于链表的操作:使用双指针交换链表中的节点成对;删除链表的倒数第N个节点;找出两个链表的相交节点;检测并定位环形链表的入口节点。这些解题策略关键在于巧妙利用指针移动和计算链表长度。
摘要由CSDN通过智能技术生成

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

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode *dummy=new ListNode();
        dummy->next=head;
        ListNode *cur=dummy;
        while(cur->next!=nullptr&&cur->next->next!=nullptr)
        {
            ListNode *temp=cur->next;
            cur->next=temp->next;
            temp->next=cur->next->next;
            cur->next->next=temp;
            cur=temp;
        }
        return dummy->next;

    }
};

注意点:在草稿纸上画清楚过程,实在不行就多用几个指针

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

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode *dummy=new ListNode();
        dummy->next=head;
        ListNode *slow=dummy;
        ListNode *fast=dummy;
        while(n--)
        {
            fast=fast->next;
        }
        while(fast->next!=nullptr)
        {
            fast=fast->next;
            slow=slow->next;
        }
        ListNode *temp=slow->next;
        slow->next=temp->next;
        delete temp;
        return dummy->next;

    }
};

注意点:
本题的精髓就是:fast比slow先走n步,当fast->next=null时,slow正好在倒数第n个的前一个;

面试题 02.07. 链表相交

class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        int lenA=0,lenB=0;
        ListNode *curA=headA;
        ListNode *curB=headB;
        while(curA!=NULL)
        {
            lenA++;
            curA=curA->next;
        }
        while(curB!=NULL)
        {
            lenB++;
            curB=curB->next;
        }
        curA=headA;
        curB=headB;
        if(lenB>lenA)
        {
            swap(curB,curA);
            swap(lenA,lenB);
        }
        int gap=lenA-lenB;
        while(gap--)
        {
            curA=curA->next;
        }
        while(curA!=NULL&&curB!=NULL)
        {
            if(curA==curB)
              return curA;
            else
            {
                curA=curA->next;
                curB=curB->next;
            }
        }
        return NULL;
        
    }
};

注意点:本题的精髓在于相交的链表末尾对齐,我们求出两个链表的长度,并求出两个链表长度的差值,然后让curA移动到,和curB 末尾对齐的位置

142. 环形链表 II

class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode *slow=head;
        ListNode *fast=head;
        while(fast!=NULL&&fast->next!=NULL)
        {
            slow=slow->next;
            fast=fast->next->next;
            if(slow==fast)
            {
                ListNode *index1=head;
                ListNode *index2=fast;
                while(index1!=index2)
                {
                    index1=index1->next;
                    index2=index2->next;
                }
                return index1;
            }
        }
        return NULL;
        
    }
};

打卡第四天
一共做了4题,需要重新做的题目有
面试题 02.07. 链表相交
142. 环形链表 II

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值