【LeetCode 10】142. 环形链表 II

【LeetCode 10】142. 环形链表 II

一、题意

二、思考过程

这道题不仅考察对链表的操作,而且还需要一些数学运算。

主要考察两知识点:

  • 判断链表是否有环
  • 如果有环,如何找到这个环的入口

以上两个为数学问题。

2.1判断链表是否有环

可以使用快慢指针法,分别定义 FastIndex指针和 SlowIndex指针,从头结点出发,FastIndex指针每次移动两个节点, SlowIndex指针每次移动一个节点,如果 FastIndex指针和 SlowIndex指针在途中相遇,说明这个链表有环。

FastIndex指针:走两步

SlowIndex指针:走一步

  while(FastIndex&&FastIndex->next){
        SlowIndex=SlowIndex->next;//慢指针走一步
        FastIndex=FastIndex->next->next;//快指针走两步

2.2寻找环的入口

这就意味着,从头结点出发一个指针,从相遇节点 也出发一个指针,这两个指针每次只走一个节点, 那么当这两个指针相遇的时候就是 环形入口的节点

在相遇节点处,定义一个指针f,在头结点处定一个指针h

fh 同时移动,每次移动一个节点, 那么他们相遇的地方就是 环形入口的节点

 if(SlowIndex==FastIndex){
            ListNode *f=FastIndex;
            ListNode *h=head;
            while(f!=h)
            {
                f=f->next;
                h=h->next;
            }
                return h;//环形入口节点
            }
        }

三、完整代码

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode *detectCycle(struct ListNode *head) {
     typedef struct ListNode ListNode;
     ListNode *FastIndex=head;
     ListNode *SlowIndex=head;

    while(FastIndex&&FastIndex->next){
        SlowIndex=SlowIndex->next;
        FastIndex=FastIndex->next->next;

        if(SlowIndex==FastIndex){
            ListNode *f=FastIndex;
            ListNode *h=head;
            while(f!=h)
            {
                f=f->next;
                h=h->next;
            }
                return h;
            }
        }
        return NULL;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodeLinghu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值