面试6题:

题目:从尾到头打印链表

输入一个链表,从尾到头打印链表每个节点的值。

 

解题代码:

# -*- 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
        if not listNode:
            return []
        res=[]
        while listNode.next is not None:
            res.append(listNode.val)
            listNode=listNode.next
        res.append(listNode.val)
        return res[::-1]

 

相关文章:

  • 2021-12-23
  • 2021-09-20
  • 2021-09-29
  • 2021-09-21
  • 2022-01-25
  • 2022-02-20
  • 2021-10-06
  • 2022-02-22
猜你喜欢
  • 2021-09-13
  • 2021-11-03
  • 2021-09-20
  • 2021-07-09
  • 2021-12-13
  • 2022-02-26
  • 2021-09-06
相关资源
相似解决方案