Home > Article > Backend Development > How to print a linked list from end to head in php (code example)
The content of this article is about how PHP can print a linked list from the end to the beginning (code example). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1. Push the reversed array after traversal and output
2.array_unshift — Insert one or more cells at the beginning of the array, insert the incoming cells to the beginning of the array array
int array_unshift ( array &$array , mixed $value1 [, mixed $... ] )
<?php class Node{ public $data; public $next; } //创建一个链表 $linkList=new Node(); $linkList->next=null; $temp=$linkList; for($i=1;$i<=10;$i++){ $node=new Node(); $node->data="aaa{$i}"; $node->next=null; $temp->next=$node; $temp=$node; } function printListFromTailToHead($linkList){ $arr=array(); $p=$linkList; while($p->next!=null){ $p=$p->next; array_unshift($arr,$p->data); } return $arr; } $arr=printListFromTailToHead($linkList); var_dump($arr);
Related recommendations:
How to implement thermal 58MM receipt printer printing in php
PHP implementation of printing binary tree code sharing from top to bottom
The above is the detailed content of How to print a linked list from end to head in php (code example). For more information, please follow other related articles on the PHP Chinese website!