您的位置:首页 > 文旅 > 旅游 > 百度域名的ip地址_二维码生成器免费版下载_广告精准推广平台_常州网站推广公司

百度域名的ip地址_二维码生成器免费版下载_广告精准推广平台_常州网站推广公司

2025/2/27 16:57:58 来源:https://blog.csdn.net/zhangsj1007/article/details/145654428  浏览:    关键词:百度域名的ip地址_二维码生成器免费版下载_广告精准推广平台_常州网站推广公司
百度域名的ip地址_二维码生成器免费版下载_广告精准推广平台_常州网站推广公司

92. 反转链表 II - 力扣(LeetCode)

相关题目:206. 反转链表 - 力扣(LeetCode)

解法:

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {
public:ListNode* reverseBetween(ListNode* head, int left, int right) {ListNode *dummyNode = new ListNode(-1);dummyNode->next = head;ListNode * tmp = head;ListNode * left_p = nullptr;ListNode * right_p = nullptr;//记录left_pre_p,right_post_p,可以节省一遍遍历操作ListNode * left_pre_p = dummyNode;ListNode * right_post_p = nullptr;int cnt = 1;while (1) {if (cnt == left - 1) {left_pre_p = tmp;}if (cnt == left) {left_p = tmp;}if (cnt == right) {right_p = tmp;}if (cnt == right + 1) {right_post_p = tmp;break;}cnt += 1;tmp = tmp->next;}reverseBetweenCore(left_p, right_p);left_pre_p->next = right_p;left_p->next = right_post_p;return dummyNode->next;}void reverseBetweenCore(ListNode * left, ListNode * right) {ListNode * pre = nullptr;ListNode * cur = left;//这个条件是错误的,因为 ListNode * post = cur->next,会改变right->next,//所以这个条件不会发生// while (cur != right->next) {//     ListNode * post = cur->next;//     cur->next = pre;//     pre = cur;//     cur = post;// }while(cur != right) {ListNode * post = cur->next;cur->next = pre;pre = cur;cur = post;}cur->next = pre;}
};

总结:

计算时间复杂度O(N),空间复杂度O(1),相关题目解法:反转链表(206)-CSDN博客。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com