题解 | #反转链表#
反转链表
http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
class Solution:
def ReverseList(self , head: ListNode) -> ListNode:
if head == None or head.next == None:
return head
cur = head
pre = None
while cur!=None:
tmp = cur.next
cur.next = pre
pre = cur
cur = tmp
return pre
反转列表需要一个pre一个tmp
先要记住下一个位置