Skip to content

Commit ead43b8

Browse files
author
pony
committed
Add remove-linked-list-elements solution
1 parent e372329 commit ead43b8

File tree

2 files changed

+199
-0
lines changed

2 files changed

+199
-0
lines changed
+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace leetcode;
6+
7+
use leetcode\util\ListNode;
8+
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
69+
{
70+
if (!$head) {
71+
return null;
72+
}
73+
$curr = $head;
74+
while ($curr->next) {
75+
if ($curr->next->val === $val) {
76+
$curr->next = $curr->next->next;
77+
} else {
78+
$curr = $curr->next;
79+
}
80+
}
81+
82+
return $head->val === $val ? $head->next : $head;
83+
}
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace leetcode\tests;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use leetcode\RemoveLinkedListElements;
9+
use leetcode\util\ListNode;
10+
11+
class RemoveLinkedListElementsTest extends TestCase
12+
{
13+
private ?ListNode $l1;
14+
private ?ListNode $l2;
15+
private ?ListNode $l3;
16+
private ?ListNode $l4;
17+
18+
protected function setUp(): void
19+
{
20+
$this->l1 = new ListNode(
21+
1,
22+
new ListNode(
23+
2,
24+
new ListNode(
25+
6,
26+
new ListNode(
27+
3,
28+
new ListNode(
29+
4,
30+
new ListNode(
31+
5,
32+
new ListNode(6)
33+
)
34+
)
35+
)
36+
)
37+
)
38+
);
39+
$this->l2 = null;
40+
$this->l3 = new ListNode(
41+
7,
42+
new ListNode(
43+
7,
44+
new ListNode(
45+
7,
46+
new ListNode(
47+
7,
48+
new ListNode(7)
49+
)
50+
)
51+
)
52+
);
53+
$this->l4 = new ListNode(9);
54+
}
55+
56+
public function testRemoveElements(): void
57+
{
58+
$node1 = RemoveLinkedListElements::removeElements($this->l1, 6);
59+
self::assertSame([1, 2, 3, 4, 5], ListNode::toArray($node1));
60+
61+
$node2 = RemoveLinkedListElements::removeElements($this->l2, 1);
62+
self::assertSame([], ListNode::toArray($node2));
63+
64+
$node3 = RemoveLinkedListElements::removeElements($this->l3, 7);
65+
self::assertSame([], ListNode::toArray($node3));
66+
67+
$node4 = RemoveLinkedListElements::removeElements($this->l4, 9);
68+
self::assertSame([], ListNode::toArray($node4));
69+
}
70+
71+
public function testRemoveElements2(): void
72+
{
73+
$node1 = RemoveLinkedListElements::removeElements2($this->l1, 6);
74+
self::assertSame([1, 2, 3, 4, 5], ListNode::toArray($node1));
75+
76+
$node2 = RemoveLinkedListElements::removeElements2($this->l2, 1);
77+
self::assertSame([], ListNode::toArray($node2));
78+
79+
$node3 = RemoveLinkedListElements::removeElements2($this->l3, 7);
80+
self::assertSame([], ListNode::toArray($node3));
81+
82+
$node4 = RemoveLinkedListElements::removeElements2($this->l4, 9);
83+
self::assertSame([], ListNode::toArray($node4));
84+
}
85+
86+
public function testRemoveElements3(): void
87+
{
88+
$node1 = RemoveLinkedListElements::removeElements3($this->l1, 6);
89+
self::assertSame([1, 2, 3, 4, 5], ListNode::toArray($node1));
90+
91+
$node2 = RemoveLinkedListElements::removeElements3($this->l2, 1);
92+
self::assertSame([], ListNode::toArray($node2));
93+
94+
$node3 = RemoveLinkedListElements::removeElements3($this->l3, 7);
95+
self::assertSame([], ListNode::toArray($node3));
96+
97+
$node4 = RemoveLinkedListElements::removeElements3($this->l4, 9);
98+
self::assertSame([], ListNode::toArray($node4));
99+
}
100+
101+
public function testRemoveElements4(): void
102+
{
103+
$node1 = RemoveLinkedListElements::removeElements4($this->l1, 6);
104+
self::assertSame([1, 2, 3, 4, 5], ListNode::toArray($node1));
105+
106+
$node2 = RemoveLinkedListElements::removeElements4($this->l2, 1);
107+
self::assertSame([], ListNode::toArray($node2));
108+
109+
$node3 = RemoveLinkedListElements::removeElements4($this->l3, 7);
110+
self::assertSame([], ListNode::toArray($node3));
111+
112+
$node4 = RemoveLinkedListElements::removeElements4($this->l4, 9);
113+
self::assertSame([], ListNode::toArray($node4));
114+
}
115+
}

0 commit comments

Comments
 (0)