-
Notifications
You must be signed in to change notification settings - Fork 0
/
141-linked-list-cycle.cpp
38 lines (32 loc) · 1.22 KB
/
141-linked-list-cycle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Title: Linked List Cycle
// Description:
// Given head, the head of a linked list, determine if the linked list has a cycle in it.
// There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer.
// Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter.
// Return true if there is a cycle in the linked list. Otherwise, return false.
// Link: https://leetcode.com/problems/linked-list-cycle/
// Time complexity: O(n)
// Space complexity: O(1)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
// Floyd's tortoise and hare algorithm
bool hasCycle(ListNode *head) {
ListNode *fastPtr = head, *slowPtr = head;
while (true) {
if (fastPtr == NULL) return false;
fastPtr = fastPtr->next;
slowPtr = slowPtr->next;
if (fastPtr == NULL) return false;
fastPtr = fastPtr->next;
if (fastPtr == slowPtr) return true;
}
}
};