Skip to content

Commit 9cf0fb6

Browse files
committed
fix: You can't insert before the first element with method insertBefore
Resolves: mirahman#4
1 parent d422be5 commit 9cf0fb6

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

DS/LinkedList/Classes/LinkedList.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public function insertBefore(string $data = NULL, string $query = NULL) {
6969
while ($currentNode !== NULL) {
7070
if ($currentNode->data === $query) {
7171
$newNode->next = $currentNode;
72+
if ($previous === NULL) {
73+
$this->_firstNode = &$newNode;
74+
} else {
75+
$previous->next = $newNode;
76+
}
7277
$previous->next = $newNode;
7378
$this->_totalNode++;
7479
break;

0 commit comments

Comments
 (0)