/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
if (head == null) {
return null;
}
ListNode dummy = new ListNode(-1);
dummy.next = head;
head = dummy;
int cnt = 1;
while (cnt < m) {
head = head.next;
cnt++;
}
ListNode ptr = head.next;
while (cnt < n) {
ListNode temp = ptr.next;
ptr.next = temp.next;
temp.next = head.next;
head.next = temp;
cnt++;
}
return dummy.next;
}
}