Skip to content

Commit

Permalink
Add solution for 'Linked List Cycle' LeetCode problem.
Browse files Browse the repository at this point in the history
  • Loading branch information
lujewwy committed Aug 31, 2023
1 parent ce22962 commit a8ec3a2
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions hw14/141_LinkedListCycle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* Definition for a singly-linked list.
* class ListNode {
* public $val = 0;
* public $next = null;
* function __construct($val) { $this->val = $val; }
* }
*/

class Solution

Check failure on line 12 in hw14/141_LinkedListCycle.php

View workflow job for this annotation

GitHub Actions / phpcs

Each class must be in a namespace of at least one level (a top-level vendor name)
{
/**
* @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;
}
}

0 comments on commit a8ec3a2

Please sign in to comment.