链表的反转

class ListNode:
     def __init__(self, x):
         self.val = x
         self.next = None

class Solution:
    # 返回从尾部到头部的列表值序列,例如[1,2,3]
    def printListFromTailToHead(self, listNode):
        # write code here
        node=listNode
        res=[]
        while node:
            res.append(node.val)
            node=node.next
        return res[::-1]

思路:将链表的节点添加进列表,再对列表进行反转输出

posted @ 2020-05-17 17:42  谁谁是谁  阅读(111)  评论(0)    收藏  举报