My solution using python

  • 0
    def reverseBetween(self, head, m, n):
        :type head: ListNode
        :type m: int
        :type n: int
        :rtype: ListNode
        if not head or not
            return head
        # add a node to avoid reverse head
        dummy = ListNode(0) = head
        pre = dummy
        for i in range(m - 1):
            pre =
        p =
        # pre is the pre node of reverselist
        # p always point the last node in the reverselist
        for i in range(n - m):
            tmp =
   = tmp
        # return the next node of dummy

Log in to reply

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