从尾到头打印链表

从尾到头打印链表

http://www.nowcoder.com/questionTerminal/d0267f7f55b3412ba93bd35cfa8e8035

简单列表保存

def printListFromTailToHead(self, listNode):
        # write code here
        res=[]
        while listNode:
            res.append(listNode.val)
            listNode=listNode.next
        return res[::-1]  #逆序打印

递归

    def printListFromTailToHead(self, listNode):
        res=[]
        def printListnode(listNode):
            # write code here
            if listNode:
                printListnode(listNode.next)#先递归到最后一层
                res.append(listNode.val)#添加值,退出函数,返回到上一层函数中的这行,继续添加值
        printListnode(listNode)
        return res
全部评论
递归的话是调用printlistnode函数,没有停止调用的条件啊
点赞 回复 分享
发布于 2020-02-24 18:15

相关推荐

评论
21
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务