Python Solution


  • 0

    alt text

    class Solution(object):
        def reverseBetween(self, head, m, n):
            """
            :type head: ListNode
            :type m: int
            :type n: int
            :rtype: ListNode
            """
            dummy = ListNode(0)
            dummy.next = head
            c1, c2 = dummy, dummy
            for _ in range(m-1):
                c1 = c1.next
            for _ in range(n+1):
                c2 = c2.next
            h = c2
            a = c1.next
            b = a.next
            while True:
                a.next = h
                if b == c2:
                    break
                h = a
                a = b
                b = b.next
            c1.next = a
            return dummy.next

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.