leetcode-141-链表题-环形链表 发表于 2020-03-09 | 分类于 数据结构与算法 题目 解法123456789101112131415161718192021222324252627/** * 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) { if(!head) return false; ListNode *slow = head; ListNode *fast = head->next; while(fast && fast->next){ if(slow == fast) return true; slow = slow->next; fast = fast->next->next; } return false; }};