-
Notifications
You must be signed in to change notification settings - Fork 0
/
剑指 Offer 06. 从尾到头打印链表.php
63 lines (56 loc) · 1.42 KB
/
剑指 Offer 06. 从尾到头打印链表.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
/**
* Created by PhpStorm
* User: jtahstu
* Time: 2022/5/7 18:10
* Des: 剑指 Offer 06. 从尾到头打印链表
* https://leetcode.cn/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/
* 输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
*/
/**
* Definition for a singly-linked list.
*/
class ListNode
{
public $val = 0;
public $next = null;
function __construct($val)
{
$this->val = $val;
}
}
class Solution
{
/**
* @param ListNode $head
* @return Integer[]
*/
function reversePrint($head)
{
//解法1
// $list = [$head->val];
// while ($head->next !== null) {
// $head = $head->next;
// $list = array_unshift($list, $head->val);
// }
// return $list;
//解法2
$list = [$head->val];
while ($head->next !== null) {
$head = $head->next;
$list[] =$head->val;
}
return array_reverse($list);
}
}
/**
* 栈
* 执行用时:208 ms, 在所有 PHP 提交中击败了7.07%的用户
* 内存消耗:21.4 MB, 在所有 PHP 提交中击败了48.48%的用户
* 通过测试用例:24 / 24
*
* 还是数组快
* 执行用时:4 ms, 在所有 PHP 提交中击败了97.98%的用户
* 内存消耗:21.3 MB, 在所有 PHP 提交中击败了76.77%的用户
* 通过测试用例:24 / 24
*/