class ListNode {
public int $val = 0;
public ?ListNode $next = null;
function __construct(?int $val = 0, ?ListNode $next = null) {
$this->val = $val;
$this->next = $next;
}
function appendToListEnd(int $val) {
if ($this->next == null) {
$this->next = new ListNode($val);
} else {
$temp = $this->next;
while ($temp->next != null) {
$temp = $temp->next;
}
$temp->next = new ListNode($val);
}
}
}
$arr = [ 1, 2, 3, 4 ];
$listHead = new ListNode($arr[0]);
for ($i = 1; $i < count($arr); $i++) {
$listHead->appendToListEnd($arr[$i]);
}
print_r($listHead);
$listHead->appendToListEnd(5);
print_r($listHead);
Answer from lukas.j on Stack OverflowPhpdoc
docs.phpdoc.org › components › guides › classes › phpDocumentor-Guides-Nodes-ListNode.html
Guides » \phpDocumentor\Guides\Nodes\ListNode
phpDocumentor · Guides · Nodes · FinalYes · ListNode.php : 17 · $classes : array<string|int, string> $options : array<string, scalar|array<string|int, scalar>|null> $value : TValue · $ordered : bool · $orderingType : string|null · $start : string|null ·
GeeksforGeeks
geeksforgeeks.org › php › implementation-of-linked-list-in-php
Implementation of Linked List in PHP - GeeksforGeeks
February 14, 2024 - <?php class ListNode { public $data = NULL; public $next = NULL; public function __construct($data = NULL) { $this->data = $data; } } class LinkedList { private $firstNode = NULL; // Add a new node to the end of the list public function insert($data) { $newNode = new ListNode($data); if ($this->firstNode === NULL) { $this->firstNode = $newNode; } else { $currentNode = $this->firstNode; while ($currentNode->next !== NULL) { $currentNode = $currentNode->next; } $currentNode->next = $newNode; } } // Traverse the list public function traverse() { $currentNode = $this->firstNode; while ($currentNode !== NULL) { echo $currentNode->data .
CodeSignal
codesignal.com › learn › courses › getting-deep-into-complex-algorithms-for-interviews-with-php › lessons › linked-list-operations-in-php
Linked List Operations in PHP
To work with linked lists in PHP, we need to define a ListNode class, which represents a node in the linked list.
Top answer 1 of 9
31
There is SplDoublyLinkedList. Is this okay, too?
2 of 9
27
Here is a linked list implementation in PHP pulled from: http://www.codediesel.com/php/linked-list-in-php/ which can add, delete, reverse and empty a linkedlist in PHP.
<?php
class ListNode
{
public $data;
public $next;
function __construct($data)
{
$this->data = $data;
$this->next = NULL;
}
function readNode()
{
return $this->data;
}
}
class LinkList
{
private $firstNode;
private $lastNode;
private $count;
function __construct()
{
$this->firstNode = NULL;
$this->lastNode = NULL;
$this->count = 0;
}
//insertion at the start of linklist
public function insertFirst($data)
{
$link = new ListNode($data);
$link->next = $this->firstNode;
$this->firstNode = &$link;
/* If this is the first node inserted in the list
then set the lastNode pointer to it.
*/
if($this->lastNode == NULL)
$this->lastNode = &$link;
$this->count++;
}
//displaying all nodes of linklist
public function readList()
{
$listData = array();
$current = $this->firstNode;
while($current != NULL)
{
array_push($listData, $current->readNode());
$current = $current->next;
}
foreach($listData as $v){
echo $v." ";
}
}
//reversing all nodes of linklist
public function reverseList()
{
if($this->firstNode != NULL)
{
if($this->firstNode->next != NULL)
{
$current = $this->firstNode;
$new = NULL;
while ($current != NULL)
{
$temp = $current->next;
$current->next =
new = $current;
$current = $temp;
}
$this->firstNode = $new;
}
}
}
//deleting a node from linklist $key is the value you want to delete
public function deleteNode($key)
{
$current = $this->firstNode;
$previous = $this->firstNode;
while($current->data != $key)
{
if($current->next == NULL)
return NULL;
else
{
$previous = $current;
$current = $current->next;
}
}
if($current == $this->firstNode)
{
if($this->count == 1)
{
$this->lastNode = $this->firstNode;
}
$this->firstNode = $this->firstNode->next;
}
else
{
if($this->lastNode == $current)
{
$this->lastNode = $previous;
}
$previous->next = $current->next;
}
$this->count--;
}
//empty linklist
public function emptyList()
{
$this->firstNode == NULL;
$this->lastNode == NULL;
$this->count = 0;
}
//insertion at index
public function insert($NewItem,$key){
if($key == 0){
$this->insertFirst($NewItem);
}
else{
$link = new ListNode($NewItem);
$current = $this->firstNode;
$previous = $this->firstNode;
for(
i<
i++)
{
$previous = $current;
$current = $current->next;
}
$previous->next = $link;
$link->next = $current;
$this->count++;
}
}
}
$obj = new LinkList();
$obj->insertFirst($value);
$obj->insert($value,$key); // at any index
$obj->deleteNode($value);
$obj->readList();
Note that some bugs have been fixed in this post. See comments.
AlphaCodingSkills
alphacodingskills.com › php › ds › php-linked-list.php
PHP - Linked List - AlphaCodingSkills
<?php //node structure class Node { public $data; public $next; } class LinkedList { public $head; //constructor to create an empty LinkedList public function __construct(){ $this->head = null; } //display the content of the list public function PrintList() { $temp = new Node(); $temp = $this->head; if($temp != null) { echo "The list contains: "; while($temp != null) { echo $temp->data."
Habr
habr.com › en › articles › 506660
Implementation of Linked List in PHP / Habr
June 14, 2020 - <?php //node structure class Node { public $data; public $next; } class LinkedList { public $head; public function __construct(){ $this->head = null; } //Add new element at the end of the list public function push_back($newElement) { $newNode = new Node(); $newNode->data = $newElement; $newNode->next = null; if($this->head == null) { $this->head = $newNode; } else { $temp = new Node(); $temp = $this->head; while($temp->next != null) { $temp = $temp->next; } $temp->next = $newNode; } } //display the content of the list public function PrintList() { $temp = new Node(); $temp = $this->head; if($temp != null) { echo "\nThe list contains: "; while($temp != null) { echo $temp->data."
Codeforces
codeforces.com › blog › entry › 78681
Implementation of Linked List in PHP - Codeforces
<?php //node structure class Node { public $data; public $next; } class LinkedList { public $head; public function __construct(){ $this->head = null; } //Add new element at the end of the list public function push_back($newElement) { $newNode = new Node(); $newNode->data = $newElement; $newNode->next = null; if($this->head == null) { $this->head = $newNode; } else { $temp = new Node(); $temp = $this->head; while($temp->next != null) { $temp = $temp->next; } $temp->next = $newNode; } } //display the content of the list public function PrintList() { $temp = new Node(); $temp = $this->head; if($temp != null) { echo "\nThe list contains: "; while($temp != null) { echo $temp->data."
GitHub
gist.github.com › 8679653
Demonstration of a PHP LinkedList, as well as a LinkedListIterator that implements \Iterator. · GitHub
Demonstration of a PHP LinkedList, as well as a LinkedListIterator that implements \Iterator. - linked-lists-and-iterators.php
AlphaCodingSkills
alphacodingskills.com › php › ds › php-count-nodes-in-the-linked-list.php
PHP - Count nodes in the Linked List - AlphaCodingSkills
<?php //node structure class Node { public $data; public $next; } class LinkedList { public $head; public function __construct(){ $this->head = null; } //Add new element at the end of the list public function push_back($newElement) { $newNode = new Node(); $newNode->data = $newElement; $newNode->next = null; if($this->head == null) { $this->head = $newNode; } else { $temp = new Node(); $temp = $this->head; while($temp->next != null) { $temp = $temp->next; } $temp->next = $newNode; } } //count nodes in the list public function countNodes() { $temp = new Node(); $temp = $this->head; $i = 0; while($temp != null) { $i++; $temp = $temp->next; } return $i; } //display the content of the list public function PrintList() { $temp = new Node(); $temp = $this->head; if($temp != null) { echo "The list contains: "; while($temp != null) { echo $temp->data."
Medium
felixrunye.medium.com › daily-leet-code-challenge-add-two-numbers-in-a-list-node-50e10238a808
Daily Leet Code Challenge: Add Two Numbers in a list node | by Felix Ivance Runye | Medium
June 8, 2023 - 0 : val) * this.next = (next===undefined ? null : next) * } */ /** * @param {ListNode} l1 * @param {ListNode} l2 * @return {ListNode} */ var addTwoNumbers = function(l1, l2) { // get all elements of the linkedlist l1 let number1 = '' let number2 = '' while(l1) { number1 = l1.val + number1 //interchange to have the intended order l1=l1.next } // get all elements of linkedlist l2 while(l2){ number2 = l2.val+number2 //interchange to have the intended order l2 = l2.next } // add the numbers and convert to a string const sum = (BigInt(number1) + BigInt(number2)).toString() let list = null let i = 0 // loop through the elements and add them to a new list while(i < sum.length){ list = new ListNode(sum[i], list) i++ } return list }; In php ·