leetcode 142. 环形链表 II

这题因为要求不使用额外空间,所以我们直接打标记就阔以了。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
const int INF=0x3f3f3f3f;
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        while (head) {
            if (head->val==INF) {
                return head;
            }
            head->val=INF;
            head=head->next;
        }
        return NULL;
    }
};
posted @ 2020-01-27 10:47  xyee  阅读(89)  评论(0编辑  收藏  举报