代码随想录算法训练营第四天|24. 两两交换链表中的节点、19.删除链表的倒数第N个节点、面试题 02.07. 链表相交、142.环形链表II

代码随想录算法训练营第四天|24. 两两交换链表中的节点、19.删除链表的倒数第N个节点、面试题 02.07. 链表相交、142.环形链表II


总结:今天的题做的很顺利,有问题可以私聊。用时一个小时。

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

题目链接:24. 两两交换链表中的节点
文章链接
思路:划分出来步骤,很快ac。

代码

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode* dummynode = new ListNode(0);  
        dummynode -> next = head;
        ListNode* cur = dummynode;
        while (cur -> next != NULL and cur -> next -> next != NULL) //下面用到了两个next和三个next,要保证前一项存在
        {
            ListNode* temp = cur -> next;
            ListNode* temp1 = cur -> next -> next -> next;

            cur -> next = cur -> next -> next;
            cur -> next -> next = temp;
            cur -> next -> next -> next = temp1;

            cur = cur -> next -> next;

        }
        return dummynode -> next;
    }
    
};

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

题目链接:19.删除链表的倒数第N个节点
文章链接
状态:知道用双指针的话能很快ac。

思路:一开始没想起来用双指针,看了眼思路很快ac出来。注意C++要释放内存。
代码

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* dummynode = new ListNode(0);
        dummynode -> next = head;
        ListNode* fast = dummynode;
        ListNode* slow = dummynode;

        while (n-- and fast != NULL)
        {
            fast = fast -> next;
        }
        fast = fast -> next;

        while (fast != NULL)
        {
            fast = fast -> next;
            slow = slow -> next;
        }

        ListNode* tmp = slow -> next;
        slow -> next = slow -> next -> next;
        delete tmp;
        return dummynode -> next;
    }
};

面试题 02.07. 链表相交

题目链接:面试题 02.07. 链表相交
文章链接
状态:花的时间有点久,可以ac。

思路:最后找交点的时候挺秒的,不好想。

代码

class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) 
    {
        ListNode* cur1 = headA;
        ListNode* cur2 = headB;
        int len1 = 0, len2 = 0;
        while (cur1 != NULL)
        {
            len1 ++;
            cur1 = cur1 -> next;
        }
        while (cur2 != NULL)
        {
            len2 ++;
            cur2 = cur2 -> next;
        }
        cur1 = headA;
        cur2 = headB;
        if (len1 < len2) swap(len1,len2),swap(cur1,cur2);

        int gap = len1 - len2;
        while (gap--)
        {
            cur1 = cur1 -> next;
        }
        while (cur1 != NULL)
        {
            if (cur1 == cur2) return cur1;
            cur1 = cur1 -> next;
            cur2 = cur2 -> next;
        }
        return NULL;

    }
};

142.环形链表II

题目链接:142.环形链表II
文章链接
状态:之前听卡哥讲的时候听懂了,所以能ac出来。

思路:我这里直接粘贴卡哥的一个公式x = (n - 1) (y + z) + z。

代码

class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode* fast = head;
        ListNode* slow = head;
        while (fast != NULL and fast -> next != NULL)
        {
            slow = slow -> next;
            fast = fast -> next -> next;
            if (slow == fast)
            {
                ListNode* index1 = fast;
                ListNode* index2 = head;
                while (index1 != index2)
                {
                    index1 = index1 -> next;
                    index2 = index2 -> next;
                }
                return index1;
            }
        }
        return NULL;
    }
};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值