定义链表
class ListNode {
public $val = NULL;
public $next = NULL;
function __construct($val = 0, $next = null) {
$this->val = $val;
$this->next = $next;
}
}
class LinkList {
public $next=null;
public function insert($val = NULL) {
$newNode = new ListNode($val);
if ($this->next === NULL) {
$this->next = $newNode;
} else {
$currentNode = $this->next;
while ($currentNode->next !== NULL) {
$currentNode = $currentNode->next;
}
$currentNode->next = $newNode;
}
}
public function insert_arr($arr = array() ) {
foreach($arr as $val){
$this->insert($val);
}
}
public function display() {
echo "当前链表的是: <br/>";
$currentNode = $this->next;
while ($currentNode !== NULL) {
echo $currentNode->val . "\n";
$currentNode = $currentNode->next;
}
}
}