SwapNodesInPairs::swapPairs()   A
last analyzed

Complexity

Conditions 3
Paths 2

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 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
use leetcode\util\ListNode;
8
9
class SwapNodesInPairs
10
{
11
    public static function swapPairs(ListNode $head): ListNode
12
    {
13
        $dummy = new ListNode();
14
        $dummy->next = $head;
15
        $curr = $dummy;
16
17
        while (($a = $curr->next) && ($b = $curr->next->next)) {
18
            $a->next = $b->next;
19
            $curr->next = $b;
20
            $curr->next->next = $a;
21
            $curr = $curr->next->next;
22
        }
23
24
        return $dummy->next;
25
    }
26
}
27