python | #反转链表#

反转链表

http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca

求指点!!!

# -*- 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 not pHead or not pHead.next: return pHead
        pre, nex = ListNode(0), ListNode(0)
        pre = pre.next
        while pHead:
            nex = pHead.next
            pHead.next = pre
            pre = pHead
            pHead = nex
        return pre

其中有一行代码

pre = pre.next

我必须使一开始的pre为None,但是ListNode要求值只能为一个数,这里有人知道别的做法吗?
另外,我这里显示用时和内存占用都只打败不超过30%的人,有使用python的大佬指教一下吗?

全部评论

相关推荐

2025-11-15 14:35
南京邮电大学 Java
程序员牛肉:你这简历有啥值得拷打的?在牛客你这种简历一抓一大把,也就是个人信息不一样而已。 关键要去找亮点,亮点啊,整个简历都跟流水线生产出来的一样。
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务