剑指offer全集详解python版——反转链表

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_41679411/article/details/86483718

题目描述:
输入一个链表,反转链表后,输出新链表的表头。。

思路:

控制好循环体就可以了,仔细一点。

代码:

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    # 返回ListNode
    def ReverseList(self, pHead):
        # write code here
        if pHead == None:
            return pHead
        f = None
        p = pHead
        n = p.next
        while n :
            tmp = n.next
            p.next = f
            n.next = p
            f = p
            p = n
            n = tmp
        return p

猜你喜欢

转载自blog.csdn.net/weixin_41679411/article/details/86483718