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
|
|
|
} |
126
|
|
|
|