File tree Expand file tree Collapse file tree 1 file changed +13
-6
lines changed Expand file tree Collapse file tree 1 file changed +13
-6
lines changed Original file line number Diff line number Diff line change @@ -283,11 +283,18 @@ $circularLinkedList->display(); // Output: 1 2 3
283
283
284
284
2 . ** Insertion at the End** : Adding a new node at the end of the linked list.
285
285
``` 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
+ }
292
299
```
293
300
You can’t perform that action at this time.
0 commit comments