Skip to content

Commit c4dc2f0

Browse files
authored
Update LINKEDLIST.md
1 parent 960db9e commit c4dc2f0

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

LINKEDLIST.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,18 @@ $circularLinkedList->display(); // Output: 1 2 3
283283

284284
2. **Insertion at the End**: Adding a new node at the end of the linked list.
285285
```php
286-
// Insertion at the beginning of the linked list
287-
public function insertAtBeginning($data) {
288-
$newNode = new Node($data);
289-
$newNode->next = $this->head;
290-
$this->head = $newNode;
291-
}
286+
// Insertion at the end of the linked list
287+
public function insertAtEnd($data) {
288+
$newNode = new Node($data);
289+
if ($this->head === null) {
290+
$this->head = $newNode;
291+
return;
292+
}
293+
$current = $this->head;
294+
while ($current->next !== null) {
295+
$current = $current->next;
296+
}
297+
$current->next = $newNode;
298+
}
292299
```
293300

0 commit comments

Comments
 (0)