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 Overflow
🌐
Phpdoc
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 .
🌐
PHP
php.net › manual › en › class.domnodelist.php
PHP: DOMNodeList - Manual
Represents a live list of nodes · The number of nodes in the list. The range of valid child node indices is 0 to length - 1 inclusive
🌐
GitHub
github.com › beldar › php-data-sctructures › blob › master › ListNode.php
php-data-sctructures/ListNode.php at master · beldar/php-data-sctructures
I'm going to all data structures I learned about in college and implementing them in PHP, both to retrieve the knowledge back and to test the limits of PHP. - php-data-sctructures/ListNode.php at master · beldar/php-data-sctructures
Author: beldar
🌐
DEV Community
dev.to › abdulwahidkahar › introduction-to-linked-lists-in-php-a-beginners-guide-5fe6
Introduction to Linked Lists in PHP: A Beginner's Guide - DEV Community
January 26, 2025 - Linked Lists are a fundamental data structure in computer science, where elements (called nodes) are connected sequentially through pointers. Unlike arrays, Linked Lists are dynamic, meaning they can grow or shrink in size without the need for ...
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.

Find elsewhere
🌐
Medium
medium.com › @fazlulkabir94 › linked-list-in-php-b677cdb06105
Linked List in PHP. A linked list is a linear data… | by Md Shohag | Medium
December 1, 2019 - Linked List in PHP A linked list is a linear data structure where each element is a separate object. Each (called it Node) element is comprising of two items data and a next reference node. The last …
🌐
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."
🌐
Doeken
doeken.org › doeke norg | php architect | legacy modernization › blog › (singly) linked lists explained in php
(Singly) Linked Lists explained in PHP | doeken.org
March 27, 2026 - Linked lists explained in PHP: a linear data structure with fast insertion and deletion, and when to prefer one over an array.
🌐
GitHub
gist.github.com › zachflower › 031e2f30f07e6c286ab8
PHP implementation of a singly linked list. · GitHub
PHP implementation of a singly linked list. GitHub Gist: instantly share code, notes, and snippets.
🌐
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
🌐
GitHub
github.com › phpzc-net › ListNode
GitHub - phpzc-net/ListNode: A PHP extension,use list struct to malloc memory,a simple extension can be used for study how to write php extension,and there is chinese comment in code. · GitHub
A PHP extension,use list struct to malloc memory,a simple extension can be used for study how to write php extension,and there is chinese comment in code. - phpzc-net/ListNode
Author: phpzc-net
🌐
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 ·