标签:return self next list app 问题 arraylist 输入 append
问题:输入一个链表,按链表从尾到头的顺序返回一个ArrayList。
解决:
#方法一:
class Solution:
def printListFromTailToHead(self, listNode):
# write code here
ArrayList = []
while listNode:
ArrayList.append(listNode.val)
listNode = listNode.next
return ArrayList[::-1]
#方法二:递归
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def printListFromTailToHead(self, listNode):
# write code here
return self.printListFromTailToHead(listNode.next)+ [listNode.val] if listNode else []
标签:return self next list app 问题 arraylist 输入 append
原文地址:https://www.cnblogs.com/gugu-da/p/13364027.html