美文网首页
leetcode92

leetcode92

作者: 七齐起器 | 来源:发表于2021-05-10 17:58 被阅读0次
    # 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
    

    相关文章

      网友评论

          本文标题:leetcode92

          本文链接:https://www.haomeiwen.com/subject/rwwidltx.html