leetcode 剑指 Offer 06. 从尾到头打印链表

反转链表的代码如下 https://blog.csdn.net/qq_42351880/article/details/88637387

# 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]:
        newhead = None
        node = None
        while head!=None:
            node = head #①
            head = head.next #②
            node.next = newhead #③
            newhead = node #④
        res = []
        while newhead!=None:
            res.append(newhead.val)
            newhead = newhead.next
        return res

图示

csdn的反转 https://blog.csdn.net/xjcvip007/article/details/54348245













种一棵树最好的时间是十年前,其次是现在。
原文地址:https://www.cnblogs.com/islch/p/13555908.html