| Total Complexity | 17 |
| Total Lines | 74 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 9 | class RemoveLinkedListElements |
||
| 10 | { |
||
| 11 | public static function removeElements(?ListNode $head, int $val): ?ListNode |
||
| 12 | { |
||
| 13 | if (!$head) { |
||
| 14 | return null; |
||
| 15 | } |
||
| 16 | $next = self::removeElements($head->next, $val); |
||
| 17 | if ($head->val === $val) { |
||
| 18 | return $next; |
||
| 19 | } |
||
| 20 | $head->next = $next; |
||
| 21 | |||
| 22 | return $head; |
||
| 23 | } |
||
| 24 | |||
| 25 | public static function removeElements2(?ListNode $head, int $val): ?ListNode |
||
| 26 | { |
||
| 27 | if (!$head) { |
||
| 28 | return null; |
||
| 29 | } |
||
| 30 | [$prev, $curr] = [null, $head]; |
||
| 31 | while ($curr) { |
||
| 32 | if ($curr->val === $val) { |
||
| 33 | if ($curr === $head) { |
||
| 34 | $curr = $head = $head->next; |
||
| 35 | } else { |
||
| 36 | $prev->next = $curr->next; |
||
| 37 | $curr = $curr->next; |
||
| 38 | } |
||
| 39 | } else { |
||
| 40 | $prev = $curr; |
||
| 41 | $curr = $curr->next; |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | return $head; |
||
| 46 | } |
||
| 47 | |||
| 48 | public static function removeElements3(?ListNode $head, int $val): ?ListNode |
||
| 49 | { |
||
| 50 | if (!$head) { |
||
| 51 | return null; |
||
| 52 | } |
||
| 53 | $dummy = new ListNode(); |
||
| 54 | $dummy->next = $head; |
||
| 55 | [$prev, $curr] = [$dummy, $head]; |
||
| 56 | while ($curr) { |
||
| 57 | if ($curr->val === $val) { |
||
| 58 | $prev->next = $curr->next; |
||
| 59 | } else { |
||
| 60 | $prev = $prev->next; |
||
| 61 | } |
||
| 62 | $curr = $curr->next; |
||
| 63 | } |
||
| 64 | |||
| 65 | return $dummy->next; |
||
| 66 | } |
||
| 67 | |||
| 68 | public static function removeElements4(?ListNode $head, int $val): ?ListNode |
||
| 83 | } |
||
| 84 | } |
||
| 85 |