for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace leetcode;
use leetcode\util\ListNode;
class RemoveLinkedListElements
{
public static function removeElements(?ListNode $head, int $val): ?ListNode
if (!$head) {
return null;
}
$next = self::removeElements($head->next, $val);
if ($head->val === $val) {
return $next;
$head->next = $next;
return $head;
public static function removeElements2(?ListNode $head, int $val): ?ListNode
[$prev, $curr] = [null, $head];
while ($curr) {
if ($curr->val === $val) {
if ($curr === $head) {
$curr = $head = $head->next;
} else {
$prev->next = $curr->next;
$curr = $curr->next;
$prev = $curr;
public static function removeElements3(?ListNode $head, int $val): ?ListNode
$dummy = new ListNode();
$dummy->next = $head;
[$prev, $curr] = [$dummy, $head];
$prev = $prev->next;
return $dummy->next;
public static function removeElements4(?ListNode $head, int $val): ?ListNode
$curr = $head;
while ($curr->next) {
if ($curr->next->val === $val) {
$curr->next = $curr->next->next;
return $head->val === $val ? $head->next : $head;