LEETCODE | PYTHON | 剑指 Offer 06. | 从尾到头打印链表
1. 题目
输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/convert-binary-number-in-a-linked-list-to-integer
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处
2. 代码
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def reversePrint(self, head: ListNode) -> List[int]:
#初始化
cur = head
res = []
#遍历存储数值
while cur:
#将数值存入数组
num = cur.val
res.insert(0,num)
cur = cur.next
return res