function reverse(root, n) {
let p,
q,
t = root; // we need this pointer because we should connect the reversed list with the rest the origin list
while (root && n >= 0) {
p = root.next;
root.next = q;
q = root;
root = p;
n--;
}
// right now. the t is the last node of reversed list and root is the start of unreserved list, connect them together
t.next = root;
return q;
}
var reverseBetween = function(head, m, n) {
// first judge the extreme cases

let dummy = new ListNode(-1);
let t = n - m; // distance between n && m
let q = dummy;

// we can get the q.next is the start of reversed linked list
// why we need m > 1? because we have a dummy as the leader of head,
// and why we need the dummy? because the reverse may be start from the index 0 :)
while (m > 1) {
q = q.next;
m--;
}
const p = reverse(q.next, t);
q.next = p;
return dummy.next;
};