反转链表的代码如下 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