本文共 592 字,大约阅读时间需要 1 分钟。
输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
class Solution: # 返回从尾部到头部的列表值序列,例如[1,2,3] def printListFromTailToHead(self, listNode): current = listNode node_list = [] while current: node_list.append(current.val) current = current.next node_list.reverse() return node_list
class Solution: # 返回从尾部到头部的列表值序列,例如[1,2,3] def printListFromTailToHead(self, listNode): # write code here if listNode is None: return [] return self.printListFromTailToHead(listNode.next) + [listNode.val]
转载地址:http://tffab.baihongyu.com/