题解:
- /**
- * 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)
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |