def reverseList(head):
if head is None or head.next is None:
return head
pre_node = head
current_node = head
while current_node is not None:
next_node = current_node.next # 暂存当前节点的next节点
current_node.next = pre_node # 当前节点的next指向初始化的节点
pre_node = current_node # 初始化的节点为当前节点
current_node = next_node # 当前节点为暂存的节点
return pre_node
python—单链表反转
最新推荐文章于 2024-08-26 23:12:47 发布