/** * 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) { ListNode* p, *cur, *temp,*insert; ListNode *dump=new ListNode(0); dump->next=head; p= dump; cur=head; for (int i = 0; i < m-1; i++) { p = cur; cur=cur->next;//先到达逆置起始点 } for(int i=m;i<n;i++){ insert=p->next; temp=cur->next; p->next=temp; cur->next=temp->next; temp->next=insert;//原地逆置 } return dump->next;//返回头节点 } };