解题思路
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
if(m == n) return head;
auto dump = new ListNode(-1);
dump->next = head;
auto a = dump , d = dump;
for(int i = 0;i < m - 1;i++) a = a->next;
for(int i = 0;i < n ; i++) d = d->next;
auto b = a->next;
auto c = d->next;
for(auto p = b ,q = b->next;q!=c;)
{
auto o = q->next;
q->next = p;
p = q,q = o;
}
b->next = c;
a->next = d;
return dump->next;
}
};