python---反转链表

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None


class Solution:
    """反转链表, 输出表头"""

    def ReverseList(self, pHead):
        # 空链表或链表只有一个结点
        if pHead is None or pHead.next is None:
            return pHead

        cur_node = pHead
        pre = None

        while cur_node is not None:
            p_next = cur_node.next
            cur_node.next = pre
            pre = cur_node
            cur_node = p_next
        return pre

作者:凯旋.Lau
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/KX-Lau/p/12539770.html