ReverseLinkedList   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 16
c 2
b 0
f 0
dl 0
loc 28
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A reverseList2() 0 10 3
A reverseList() 0 14 3
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