Test Failed
Push — master ( f38782...f110ed )
by Jinyun
02:42
created

LinkedListCycleII   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 14
c 1
b 0
f 0
dl 0
loc 22
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A detectCycle() 0 20 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
use leetcode\util\ListNode;
8
9
class LinkedListCycleII
10
{
11
    public static function detectCycle(?ListNode $head): ?ListNode
12
    {
13
        if (!$head) {
14
            return null;
15
        }
16
        $slow = $fast = $head;
17
        while ($fast->next && $fast->next->next) {
18
            $slow = $slow->next;
19
            $fast = $fast->next->next;
20
            if ($slow === $fast) {
21
                $curr = $head;
22
                while ($curr !== $slow) {
23
                    $curr = $curr->next;
24
                    $slow = $slow->next;
25
                }
26
                return $slow;
27
            }
28
        }
29
30
        return null;
31
    }
32
}
33