//递归版本
function printListFromTailToHead($head)
{
if($head == NULL){
return [];
}
$arr = array();
$cur = $head;
if($cur->next != null){
$arr = printListFromTailToHead($cur->next);
}
array_push($arr, $cur->val);
return $arr;
}
//非递归版本
function printListFromTailToHead($head)
{
if($head == NULL){
return [];
}
$cur = $head;
$arr = array();
$re = array();
while($cur != NULL){
array_push($arr, $cur->val);
$cur = $cur ->next;
}
while(!empty($arr)){
$tmp = array_pop($arr);
array_push($re, $tmp);
}
return $re;
}