链表反转python

Posted 一条图图犬

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了链表反转python相关的知识,希望对你有一定的参考价值。

def reverse_node_list(head):
    if not head or not head.next:
        return head
    prev = None
    while head:
        curr = head
        head = head.next
        curr.next = prev
        prev = curr

    return prev

设置三个指针, prev指向前一个节点, head 指向现在的节点, curr指向下一个要去的节点
初始化:
prev空
head表头

先保留当前节点
挪动指针
当前节点反转
挪动prev







以上是关于链表反转python的主要内容,如果未能解决你的问题,请参考以下文章