题解:
/*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/
struct ListNode* reverseBetween(struct ListNode* head, int left, int right) {struct ListNode* dummy=(struct ListNode*)malloc(sizeof(struct ListNode));dummy->next=head;struct ListNode *newHead,*temp,*steady=head,*last=dummy;for(int i=0;i<left-1;i++){steady=steady->next;last=last->next;}newHead=steady;temp=steady->next;for(int i=0;i<right-left;i++){steady->next=temp->next;temp->next=newHead;newHead=temp;temp=steady->next;}last->next=newHead;return dummy->next;
}
结果:时间复杂度O(N),空间复杂度O(1)