class Solution { public: ListNode* reverseBetween(ListNode* head, int left, int right) { ListNode *sentinel = new ListNode(-1); sentinel->next = head; auto p = sentinel; for (int i = 0; i < left - 1; i++ ) { p = p->next; } auto pre = p->next, post = p->next->next; for (int i = 0; i < right - left; i++ ) { auto temp = post->next; post->next = pre; pre = post; post = temp; } p->next->next = post; p->next = pre; return sentinel->next; } };