设定一个空指针和一个头指针,当头指针不为空时,执行:
1、暂存后继指针
2、修改后继指针
3、存储当前节点
4、访问下一节点
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
p,pre = head, None
while p:
tmp = p.next
p.next = pre
pre = p
p = tmp
return pre