类型:链表
-
从尾到头打印链表 💚
https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/
❓ 输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
💡 递归
class Solution: def reversePrint(self, head: ListNode) -> List[int]: if not head: return [] ans = [] def rec(node): if not node: return rec(node.next) ans.append(node.val) rec(head) return ans时间复杂度:O(n),空间复杂度:O(n)
💡 栈
利用先进后出的特性。
时间复杂度:O(n),空间复杂度:O(n)