算法 - 链的操作(五) - 反转链表

反转链表

输入链 : 1 -> 3 -> 5 -> 6 -> 8

输出    :   8 -> 6 -> 5 -> 3 -> 1

(python 2.7)

def reverse(head):

    if(head==None or head.next == None):
        return head

    reverseNode = None
    node = head
    pre_node = None

    while(node != None):

        nodeNext = node.next

        if(nodeNext == None):
            reverseNode = node

        node.next = pre_node
        pre_node = node
        node = nodeNext

    return reverseNode

 

posted @ 2018-03-26 18:49  抽象Java  阅读(300)  评论(0编辑  收藏  举报