输入:1->2->3->4
输出:4->3->2->1# class ListNode:# def __init__(self, x):# self.val = x# self.next = NonedefReverseList(head):
cur = head
p = cur.next
cur.next=None#这一步十分关键,总是忘记,导致无法通过,需要把头节点的next节点指向空while p:
q = p.next
p.next= cur
cur = p
p = q
return cur