ReverseLinkedList::reverseList()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 9
c 2
b 0
f 0
dl 0
loc 14
rs 9.9666
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
use leetcode\util\ListNode;
8
9
class ReverseLinkedList
10
{
11
    public static function reverseList(?ListNode $head): ?ListNode
12
    {
13
        if (!$head) {
14
            return null;
15
        }
16
        $node = null;
17
        while ($head) {
18
            $curr = $head;
19
            $head = $head->next;
20
            $curr->next = $node;
21
            $node = $curr;
22
        }
23
24
        return $node;
25
    }
26
27
    public static function reverseList2(?ListNode $head): ?ListNode
28
    {
29
        if ($head === null || $head->next === null) {
30
            return $head;
31
        }
32
        $node = self::reverseList($head->next);
33
        $head->next->next = $head;
34
        $head->next = null;
35
36
        return $node;
37
    }
38
}
39