要求
![](https://img-blog.csdnimg.cn/9da54799d8d44870a706b7f71baedc46.png)
代码
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @param m int整型
* @param n int整型
* @return ListNode类
*/
struct ListNode* reverseBetween(struct ListNode* head, int m, int n ) {
// write code here
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* temp = (struct ListNode*)malloc(sizeof(struct ListNode));
temp->next = head;
//前一个节点
struct ListNode* Pre = temp;
//当前节点
struct ListNode* Cur = head;
//下一个节点
struct ListNode* Nex;
//找到m
for (int i = 1; i < m; ++i) {
Pre = Cur;
Cur = Cur->next;
}
//现在Pre指向m节点处
//从m反转到n
for (int i = m; i < n; ++i) {
Nex = Cur->next;//这里是定义了Nex是Cur的下一个节点
Cur->next=Nex->next;//
Nex->next = Pre->next;
Pre->next = Nex;
}
return temp->next;
}