首页 > 编程语言 > 详细

链表反转python

时间:2018-03-12 19:49:57      阅读:210      评论:0      收藏:0      [点我收藏+]
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

原文:https://www.cnblogs.com/theodoric008/p/8550886.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!