链表-leetcode 142 Linked List Cycle II

本文介绍了一种高效检测链表是否存在环,并找到环起始节点的方法。利用快慢指针技术,在不增加额外空间复杂度的情况下实现了目标。文章详细解释了算法原理,并提供了简洁的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题链接:Linked List Cycle II

如果没有follow up 这道题会非常简单,也非常容易想到,代码如下:

/**
 * 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) {
        /*
            遍历链表,辅助set,如果遍历到的节点存在set中,说明循环开始了,返回,否则将节点插入到set中。否则直至遍历完,返回NULL;
            Time Complexity:O(N)
            Space Complexity:O(N)
        */
        if(!head || !head->next)return NULL;
        set<ListNode*>liset;
        while(head){
            if(liset.find(head)!=liset.end())return head;
            else{
                liset.insert(head);
                head=head->next;
            }
        }
        return NULL;
    }
};

但是有了follow up的限制之后,不允许使用额外的空间,就需要点技巧了。有这么个方法:使用快慢指针,当快慢指针第一次相遇之后,将慢指针移动回头结点,然后以慢指针的速度移动快慢指针,再次相遇就是环初始节点。证明其实也很简单,可以参考(出处)
证明

明白了思路,代码就很简单了,如下

/**
 * 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) {
        /*
            使用快慢指针,如果存在环,当快慢指针第一次相遇,将慢指针移回头结点,在以同样的速度移动快慢指针,再次相遇即是环初始节点
            Time Complexity:O(2a+b) (a:头结点到环初始节点距离,b:环初始节点到相遇节点的距离)
        */
        if(!head || !head->next)return NULL;
        ListNode* slow=head->next,*fast=head->next->next;
        while(fast && fast->next){
            if(fast==slow){
                slow=head;
                while(true){
                    if(slow==fast)return slow;
                    slow=slow->next;
                    fast=fast->next;
                }
            }
            slow=slow->next;
            fast=fast->next->next;
        }
        return NULL;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值