题目描述
输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
解题思路:
将链表中从头到尾的各个节点中的值存在list中,再逆序返回。
# -*- 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
res = []
ptr = listNode
if listNode:
while ptr:
res.append(ptr.val)
ptr = ptr.next
return res[::-1]
转载地址:https://blog.csdn.net/qq_34364995/article/details/81135662