原题截图

<?php
/*class ListNode{
var $val;
var $next = NULL;
function __construct($x){
$this->val = $x;
}
}*/
function printListFromTailToHead($head)
{
}
本题考点
对链表结构的熟悉,对栈结构的熟悉
php解题代码
function printListFromTailToHead($head)
{
if ($head === NULL) return [];
$stack = [];
$node = $head;
while ($node !== NULL) {
array_unshift($stack, $node->val);
$node = $node->next;
}
return $stack;
}
//递归版本
function printListFromTailToHead($head)
{
if ($head === NULL) return [];
$arr = [];
$node = $head;
if ($node->next !== NULL)
$arr = printListFromTailToHead($node->next);
array_push($arr, $node->val);
return $arr;
}
很好的一道题值得学习