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的大佬指教一下吗?

