其他分享
首页 > 其他分享> > AcWing 34. 链表中环的入口结点

AcWing 34. 链表中环的入口结点

作者:互联网

题目链接:https://www.acwing.com/problem/content/86/

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *entryNodeOfLoop(ListNode *head) {
        auto slow = head, fast = head;
        while(fast && fast->next){
            slow = slow->next;
            fast = fast->next;
            if(fast->next) fast = fast->next;
            else return NULL;
            if(fast == slow){
                fast = head;
                while(fast != slow){
                    fast = fast->next;
                    slow = slow->next;
                }
                return fast;
            }
        }
        return NULL;
    }
};

  

标签:head,slow,ListNode,fast,next,链表,return,34,AcWing
来源: https://www.cnblogs.com/xjtu-yzk/p/16392292.html