# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None
classSolution: # @param head, a ListNode # @param m, an integer # @param n, an integer # @return a ListNode defreverseBetween(self, head, m, n): dummy=ListNode(-1) dummy.next=head prev=dummy for i in range(m-1): prev=prev.next head2=prev prev=head2.next cur=prev.next for i in range(n-m): prev.next=cur.next cur.next=head2.next head2.next=cur cur=prev.next return dummy.next