class Solution:
def printListFromTailToHead(self, listNode):
# write code here
res = ListNode(0) # 亚节点,也是伪头部
curr = res
while listNode:
nt_nd = res.next # 记录真头部
res.next = listNode # 在亚节点之后插入新的真头部
temp = listNode.next # 记录远链表的后一个节点指针
listNode.next = nt_nd # 老的头部插入新的头部之后
listNode = temp # 原链表指针后移
return res.next
反转链表
最新推荐文章于 2024-07-10 17:20:30 发布