Test Failed
Push — master ( 395406...25b902 )
by Jinyun
02:26 queued 11s
created

ReverseLinkedListII::reverseBetween()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 27
rs 8.6506
cc 7
nc 5
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
use leetcode\util\ListNode;
8
9
class ReverseLinkedListII
10
{
11
    public static function reverseBetween(?ListNode $head, int $left, int $right): ?ListNode
12
    {
13
        if (!$head || $left <= 0 || $right <= 0 || $left > $right) {
14
            return null;
15
        }
16
        $dummy = new ListNode();
17
        $dummy->next = $head;
18
        $prev = $dummy;
19
        $curr = $dummy->next;
20
        $i = 1;
21
        while ($i < $left) {
22
            $prev = $curr;
23
            $curr = $curr->next;
24
            $i++;
25
        }
26
        $node = $prev;
27
        while ($i <= $right) {
28
            $temp = $curr->next;
29
            $curr->next = $prev;
30
            $prev = $curr;
31
            $curr = $temp;
32
            $i++;
33
        }
34
        $node->next->next = $curr;
35
        $node->next = $prev;
36
37
        return $dummy->next;
38
    }
39
40
    public static function reverseBetween2(?ListNode $head, int $left, int $right): ?ListNode
41
    {
42
        if (!$head || $left <= 0 || $right <= 0 || $left > $right) {
43
            return null;
44
        }
45
        $dummy = new ListNode();
46
        $dummy->next = $head;
47
        $prev = $dummy;
48
        for ($i = 0; $i < $left - 1; $i++) {
49
            $prev = $prev->next;
50
        }
51
        $tail = $prev->next;
52
53
        for ($i = 0; $i < $right - $left; $i++) {
54
            $temp = $prev->next;
55
            $prev->next = $tail->next;
56
            $tail->next = $tail->next->next;
57
            $prev->next->next = $temp;
58
        }
59
60
        return $dummy->next;
61
    }
62
63
    public static function reverseBetween3(?ListNode $head, int $left, int $right): ?ListNode
64
    {
65
        if (!$head || $left <= 0 || $right <= 0 || $left > $right) {
66
            return null;
67
        }
68
        $dummy = new ListNode();
69
        $dummy->next = $head;
70
        $prev = $dummy;
71
        for ($i = 1; $i < $left; $i++) {
72
            $prev = $prev->next;
73
        }
74
75
        $node = $prev->next;
76
        for ($i = $left; $i < $right; $i++) {
77
            self::swap($prev, $node->next);
0 ignored issues
show
Bug introduced by
It seems like $prev can also be of type null; however, parameter $p of leetcode\ReverseLinkedListII::swap() does only seem to accept leetcode\util\ListNode, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
            self::swap(/** @scrutinizer ignore-type */ $prev, $node->next);
Loading history...
Bug introduced by
It seems like $node->next can also be of type null; however, parameter $q of leetcode\ReverseLinkedListII::swap() does only seem to accept leetcode\util\ListNode, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
            self::swap($prev, /** @scrutinizer ignore-type */ $node->next);
Loading history...
78
            self::swap($prev, $node);
79
        }
80
81
        return $dummy->next;
82
    }
83
84
    public static function reverseBetween4(?ListNode $head, int $left, int $right): ?ListNode
85
    {
86
        if (!$head || $left <= 0 || $right <= 0 || $left > $right) {
87
            return null;
88
        }
89
        if ($left === $right) {
90
            return $head;
91
        }
92
        if ($left > 1) {
93
            $node = $head;
94
            $node->next = self::reverseBetween4($head->next, $left - 1, $right - 1);
95
            return $node;
96
        } else {
97
            $next = $head->next;
98
            $node = self::reverseBetween4($next, 1, $right - 1);
99
            $tail = $next->next;
100
            $next->next = $head;
101
            $head->next = $tail;
102
            return $node;
103
        }
104
    }
105
106
    private static function swap(ListNode $p, ListNode $q)
107
    {
108
        $t = $p->next;
109
        $p->next = $q->next;
110
        $q->next = $t;
111
    }
112
}
113