LeetCode HOT100 - 环形链表

看过 II 这题就没什么要说的了

返回 bool 类型即可

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */

 /**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) {
        ListNode *fa = head, *sl = head;
        while (1) {
            if (!fa || !fa->next) {
                return 0;
            }
            fa = fa->next->next;
            sl = sl->next;
            if (fa == sl) {
                break;
            }
        }
        return 1;
    }
};
posted @ 2026-03-22 19:29  rdcamelot  阅读(1)  评论(0)    收藏  举报