classSolution(object):defreverseList(self, head):"""
:type head: ListNode
:rtype: ListNode
"""
p =Nonewhile head:
tmp = head
head = head.next
tmp.next= p
p = tmp
return p
Reverse Linked List II
classSolution(object):defreverseBetween(self, head, m, n):"""
:type head: ListNode
:type m: int
:type n: int
:rtype: ListNode
"""ifnot head ornot head.next:return head
res=ListNode(0)
res.next=head
p=res
count=m
while count>1:
p=p.next
count-=1#要转换的开头是q,长度为n-m+1,保存p,q
q=p.next
p.next=None
count=n-m+1
n1=q
n2=n1.next
q.next=None#三指针逆序,头n1,尾巴q,下一个节点n2while count>1:
n3=n2.next
n2.next=n1
n1=n2
n2=n3
count-=1
p.next=n1
q.next=n2
return res.next