/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : val(x), next(nullptr) {} * }; */ class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @param m int整型 * @param n int整型 * @return ListNode类 */ ListNode* reverseBetween(ListNode* head, int m, int n) { vector<ListNode*> v; ListNode* cur = head; while(cur) { v.push_back(cur); cur = cur->next; } for(int i=m-1,j=m; j<=n-1 ; ++i,++j) { v[j]->next = v[i]; } ListNode* L = m>1 ? v[m-2] : nullptr; ListNode* R = n==v.size() ? nullptr : v[n]; if(L != nullptr) { L->next = v[n-1]; v[m-1]->next = R; return head; } else { v[m-1]->next = R; return v[n-1]; } } };