142. 环形链表

  1. 快慢指针,好像和之前的某道题相似
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode *detectCycle(struct ListNode *head) {
    // 快慢指针
    struct ListNode *fast=head;
    struct ListNode *low=head;
    struct ListNode *newlow=head;
    while(fast && low && fast->next && fast->next->next && low->next )
    {
        fast=fast->next->next;
        low=low->next;
        if(fast==low)
        {
            while(newlow && low)
            {
                if(newlow==low)
                {
                    return newlow;
                }
                newlow=newlow->next;
                low=low->next;
            }
        }
    }
    return NULL;
}

我看还有哈希表的解法,明天看看
https://leetcode-cn.com/problems/linked-list-cycle-ii/solution/chun-cjie-jue-ha-xi-zhi-linked-list-cycle-ii-by-we/

ε=(´ο`*)))唉,这几天好丧啊,快点调整过来吧,天涯何处无芳草

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值