class Node {
public $val;
public $next;
function __construct($val) {
$this->val = $val;
$this->next = $null;
}
}
class LinkedList {
public $head;
function __construct() {
$this->head = null;
}
function InsertNode($val) {
$node = new Node($val);
if ($this->head) {
$current = $this->head;
while ($current != null) {
$prev = $current;
$current = $current->next;
}
$prev->next = $node;
} else {
$this->head = $node;
}
}
function PrintAll() {
$current = $this->head;
while ($current != null) {
print $current->val . "\n";
$current = $current->next;
}
}
}
$linkedList = new LinkedList();
$linkedList->InsertNode(1);
$linkedList->InsertNode(3);
$linkedList->InsertNode(2);
$linkedList->PrintAll();