本文共 611 字,大约阅读时间需要 2 分钟。
输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
示例 1:
输入:head = [1,3,2]
输出:[2,3,1]限制:
0 <= 链表长度 <= 10000
# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def reversePrint(self, head: ListNode) -> List[int]: res = self.getValue(head) # print(res) return res def getValue(self,head:ListNode): if( head is None ): return [] else: # 获取返回的列表,从最后一个开始返回,进行递归 res = self.getValue(head.next) # 添加该层的值 res.append(head.val) return res
转载地址:http://fggcz.baihongyu.com/