Reverse a linked list from position m to n. Do it in one-pass.
Note: 1 ≤ m ≤ n ≤ length of list.
Example:
Input: 1->2->3->4->5->NULL, m = 2, n = 4
Output: 1->4->3->2->5->NULL
def reverseBetween(self, head, m, n):
"""
:type head: ListNode
:type m: int
:type n: int
:rtype: ListNode
"""
if m==n:
return head
dummy=pre=ListNode(0)
dummy.next,pre.next=head,head
for i in range(m-1):
pre=pre.next
reverse = None
cur=pre.next
for i in range(n-m+1):
tmp=cur.next
cur.next=reverse
reverse=cur
cur=tmp
pre.next.next=cur
pre.next=reverse
return dummy.next