Skip to content

Commit e9a96ec

Browse files
committed
Add design-linked-list solution
1 parent aa116fb commit e9a96ec

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed

src/leetcode/DesignLinkedList.php

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace leetcode;
6+
7+
use leetcode\util\ListNode;
8+
9+
class DesignLinkedList
10+
{
11+
private int $size;
12+
13+
private ?ListNode $head;
14+
15+
/**
16+
* Initialize your data structure here.
17+
*/
18+
public function __construct()
19+
{
20+
$this->size = 0;
21+
$this->head = null;
22+
}
23+
24+
/**
25+
* Get the value of the index-th node in the linked list.
26+
* If the index is invalid, return -1.
27+
*
28+
* @param int $index
29+
*
30+
* @return int
31+
*/
32+
public function get(int $index): int
33+
{
34+
if ($index < 0 || $index >= $this->size) {
35+
return -1;
36+
}
37+
$curr = $this->head;
38+
for ($i = 0; $i < $index; $i++) {
39+
$curr = $curr->next;
40+
}
41+
42+
return $curr->val;
43+
}
44+
45+
/**
46+
* Add a node of value val before the first element of the linked list.
47+
* After the insertion, the new node will be the first node of the linked list.
48+
*
49+
* @param int $val
50+
*
51+
* @return null
52+
*/
53+
public function addAtHead(int $val): void
54+
{
55+
$this->addAtIndex(0, $val);
56+
}
57+
58+
/**
59+
* Append a node of value val to the last element of the linked list.
60+
*
61+
* @param int $val
62+
*
63+
* @return null
64+
*/
65+
public function addAtTail($val): void
66+
{
67+
$this->addAtIndex($this->size, $val);
68+
}
69+
70+
/**
71+
* Add a node of value val before the index-th node in the linked list.
72+
* If index equals to the length of linked list,
73+
* the node will be appended to the end of linked list.
74+
* If index is greater than the length, the node will not be inserted.
75+
*
76+
* @param int $index
77+
* @param int $val
78+
*
79+
* @return null
80+
*/
81+
public function addAtIndex(int $index, int $val): void
82+
{
83+
if ($index > $this->size) {
84+
return;
85+
}
86+
$node = new ListNode($val);
87+
$curr = $this->head;
88+
89+
if ($index <= 0) {
90+
$node->next = $curr;
91+
$this->head = $node;
92+
} else {
93+
for ($i = 0; $i < $index - 1; $i++) {
94+
$curr = $curr->next;
95+
}
96+
$node->next = $curr->next;
97+
$curr->next = $node;
98+
}
99+
$this->size++;
100+
}
101+
102+
/**
103+
* Delete the index-th node in the linked list, if the index is valid.
104+
*
105+
* @param int $index
106+
*
107+
* @return null
108+
*/
109+
public function deleteAtIndex(int $index): void
110+
{
111+
if ($index < 0 || $index >= $this->size) {
112+
return;
113+
}
114+
$curr = $this->head;
115+
if ($index === 0) {
116+
$this->head = $curr->next;
117+
} else {
118+
for ($i = 0; $i < $index - 1; $i++) {
119+
$curr = $curr->next;
120+
}
121+
$curr->next = $curr->next->next;
122+
}
123+
$this->size--;
124+
}
125+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace leetcode\tests;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use leetcode\DesignLinkedList;
9+
10+
class DesignLinkedListTest extends TestCase
11+
{
12+
public function testDesignLinkedList(): void
13+
{
14+
$object = new DesignLinkedList();
15+
self::assertSame(-1, $object->get(3));
16+
17+
$object->addAtHead(1);
18+
self::assertSame(1, $object->get(0));
19+
20+
$object->addAtTail(3);
21+
self::assertSame(3, $object->get(1));
22+
23+
$object->addAtIndex(1, 2);
24+
self::assertSame(2, $object->get(1));
25+
26+
$object->deleteAtIndex(1);
27+
self::assertSame(1, $object->get(0));
28+
self::assertSame(3, $object->get(1));
29+
self::assertSame(-1, $object->get(2));
30+
}
31+
32+
public function testDesignLinkedList2(): void
33+
{
34+
$object = new DesignLinkedList();
35+
$object->addAtHead(1);
36+
self::assertSame(1, $object->get(0));
37+
38+
$object->addAtTail(3);
39+
self::assertSame(3, $object->get(1));
40+
41+
$object->addAtIndex(1, 2);
42+
self::assertSame(2, $object->get(1));
43+
44+
$object->deleteAtIndex(0);
45+
self::assertSame(2, $object->get(0));
46+
}
47+
}

0 commit comments

Comments
 (0)