题目:输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
Python:
解法:使用列表插入即可,每次只插入首位。
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# 返回从尾部到头部的列表值序列,例如[1,2,3]
def printListFromTailToHead(self, listNode):
# write code here
result = [] #空列表
head = listNode #列表序列
while head:
result .insert(0,head.val)
#list.insert(index, obj),index --对象obj需要插入的索引位置, obj --要插入列表中的对象。
head= head.next
return result
19/04/07