/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
if (head == null || m >= n) return head;
ListNode dummy = new ListNode(0);
dummy.next = head;
head = dummy;
for (int i = 1; i < m; i++) {
if (head == null) return null;
head = head.next;
}
ListNode premNode = head;
ListNode mNode = head.next;
ListNode nNode = mNode;
ListNode postNode = nNode.next;
for (int i = m; i < n; i++) {
if (postNode == null) return null;
ListNode tmp = postNode.next;
postNode.next = nNode;
nNode = postNode;
postNode = tmp;
}
mNode.next = postNode;
premNode.next = nNode;
return dummy.next;
}
}