/**
* 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 b
if(head == NULL || head ->next ==NULL){
return head;
}
struct ListNode* dummy = (struct ListNode*)malloc(sizeof(struct ListNode));
dummy->next = head;
struct ListNode* prev = dummy;
for(int i = 1;i<m;i++){
prev = prev -> next;
}
struct ListNode* curr = prev->next;
for(int i = 0;i<n-m;i++){
struct ListNode *next = curr->next;
curr ->next = next->next;
next->next = prev ->next;
prev ->next = next;
}
struct ListNode *newHead = dummy ->next;
free(dummy);
return newHead;
}