首页 > 其他 > 详细

从尾到头打印链表

时间:2019-10-11 17:04:14      阅读:99      评论:0      收藏:0      [点我收藏+]

剑指offer

从尾到头打印链表

输入一个链表,按链表值从尾到头的顺序返回一个ArrayList

 1 # -*- coding:utf-8 -*-
 2 # class ListNode:
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.next = None
 6 
 7 class Solution:
 8     # 返回从尾部到头部的列表值序列,例如[1,2,3]
 9     def printListFromTailToHead(self, listNode):
10         # write code here
11         result = []
12         head = listNode
13         while head is not None:
14             result.append(head.val)
15             head = head.next
16         result.reverse()  # 反向列表
17         return result

 

从尾到头打印链表

原文:https://www.cnblogs.com/gengyufei/p/11655235.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!