反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。
说明:
1 ≤ m ≤ n ≤ 链表长度。
示例:
输入: 1->2->3->4->5->NULL, m = 2, n = 4输出: 1->4->3->2->5->NULL
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseBetween(ListNode* head, int m, int n) {
        if(head == NULL){
            return head;
        }
        ListNode* sentinel = new ListNode(-1);
        sentinel->next = head;
        ListNode* last = NULL;
        ListNode* cur = sentinel;
        ListNode* nex;
        for(int i=0;i<m;i++){
            last = cur;
            cur = cur->next;
        }
        ListNode* pre = NULL;
        ListNode* end = cur;
        for(int i = m;i<=n;i++){
            nex = cur->next;
            cur->next = pre;
            pre = cur;
            cur = nex;
        }
        last->next = pre;
        end->next = nex;
        return sentinel->next;
    }
};
                    