题目
LeetCode - 141. Linked List Cycle
题目链接
https://leetcode.com/problems/linked-list-cycle/
参考博客
解题思路
常规解题思路,Two pointer方法。
解题源码
/**
* 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 *slow = head, *fast = head;
while (fast && fast->next){
slow = slow->next;
fast = fast->next->next;
if (slow == fast) return true;
}
return false;
}
};