之前刷LeetCode就已经把链表题做了不少了,今天把剑指里的链表一次性全部刷完,为了博客的完整性,即使很简单的题目,也都记录下来了,比如这题。。
# -*- 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 = []
cur = listNode
while cur is not None:
res.append(cur.val)
cur = cur.next
return res[::-1]
没啥好方法,大家都这么做的。。