Leetcode 240.剑指 Offer 06. 从尾到头打印链表 (每日一题 20210728)
生活随笔
收集整理的這篇文章主要介紹了
Leetcode 240.剑指 Offer 06. 从尾到头打印链表 (每日一题 20210728)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
輸入一個鏈表的頭節點,從尾到頭反過來返回每個節點的值(用數組返回)。示例 1:輸入:head = [1,3,2]
輸出:[2,3,1]限制:0 <= 鏈表長度 <= 10000題目地址:https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/class Solution:def reversePrint(self, head:ListNode) -> list[int]:# 第一種解法return self.reversePrint(head.next) + [head.val] if head else []# 第二種解法stack = []while head:stack.append(head.val)head = head.nextreturn stack[::-1]
總結
以上是生活随笔為你收集整理的Leetcode 240.剑指 Offer 06. 从尾到头打印链表 (每日一题 20210728)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leetcode 240.搜索二维矩阵I
- 下一篇: Leetcode 54 螺旋矩阵 (每日