diff --git a/hw14/141_LinkedListCycle.php b/hw14/141_LinkedListCycle.php new file mode 100644 index 000000000..e40487ded --- /dev/null +++ b/hw14/141_LinkedListCycle.php @@ -0,0 +1,38 @@ +val = $val; } + * } + */ + +class Solution +{ + /** + * @param ListNode $head + * @return Boolean + */ + public function hasCycle(ListNode $head): bool + { + if ($head === null) { + return false; + } + + $node = $head; + $nextNode = $head->next; + + while ($node !== $nextNode) { + if ($node === null || $nextNode === null) { + return false; + } + + $node = $node->next; + $nextNode = $nextNode->next->next; + } + + return true; + } +}