# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def reverseBetween(self, head, left, right):
"""
:type head: ListNode
:type left: int
:type right: int
:rtype: ListNode
"""
if left==right:return head
aptr=head
m=1
h1=None
h2=None
while aptr!=None:
if m==left:
h1=aptr
if m==right:
h2=aptr
aptr=aptr.next
m+=1
h3=h2.next
h2.next=None
h4=self.reverseList(h1)
aptr=h2
while aptr.next!=None:
if aptr.next==h2:
break
aptr=aptr.next
if h3!=None:
aptr.next=h3
if left>1:
aptr=head
while aptr!=None:
if aptr.next==h1:
break
aptr=aptr.next
if aptr!=None:
aptr.next=h2
else:
head=h2
return head
def reverseList(self,aptr):
if aptr.next==None:return aptr
last=self.reverseList(aptr.next)
aptr.next.next=aptr
aptr.next=None
return last
网友评论