https://leetcode.com/problems/linked-list-cycle/
리스트가 순환 하는지 여부를 return 하라. 앞서 풀었던 fast, slow node를 이용한다.
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode* fastNode = head;
ListNode* slowNode = head;
while (fastNode && fastNode->next)
{
fastNode = fastNode->next->next;
slowNode = slowNode->next;
if (fastNode == slowNode)
{
return true;
}
}
return false;
}
};
Runtime: 16 ms, faster than 23.36% of C++ online submissions for Linked List Cycle.
Memory Usage: 8 MB, less than 21.89% of C++ online submissions for Linked List Cycle.