码迷,mamicode.com
首页 > 编程语言 > 详细

python---反转链表

时间:2020-03-21 16:27:48      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:sel   ever   solution   反转链表   self   one   list   class   表头   

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

python---反转链表

标签:sel   ever   solution   反转链表   self   one   list   class   表头   

原文地址:https://www.cnblogs.com/KX-Lau/p/12539770.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!