leetcode-----142. 环形链表 II

代码

/*
 * @lc app=leetcode.cn id=142 lang=cpp
 *
 * [142] 环形链表 II
 */

// @lc code=start
/**
 * 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) {
        if (!head || !head->next) return NULL;
        auto s = head, f = head->next;
        while (f) {
            s = s->next;
            f = f->next;
            if (!f) return NULL;
            f = f->next;
            if (s == f) {
                s = head;
                f = f->next;
                while (s != f) s = s->next, f = f->next;
                return s;
            }
        } 
        return NULL;
    }
};
// @lc code=end
posted @ 2020-08-04 10:02  景云ⁿ  阅读(59)  评论(0编辑  收藏  举报