/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
class Solution {
public:
ListNode *reverseBetween(ListNode *head, int m, int n) {
ListNode *L = new ListNode(0);
L->next = head;
ListNode *p = L;
ListNode *q = head;
for(int i=1;i<m;i++) {
p = q;
q = q->next; // 找到反转的起始位置
}
for(int i=0;i<n-m;i++) { // 反转
ListNode *t = q->next;
q->next = t->next;
t->next = p->next;
p->next = t;
}
return L->next;
}
};


京公网安备 11010502036488号