当前位置: 首页> 健康> 科研 > 河南建设集团有限公司_代驾app软件开发_网站优化提升排名_北京网络seo经理

河南建设集团有限公司_代驾app软件开发_网站优化提升排名_北京网络seo经理

时间:2025/7/18 2:02:49来源:https://blog.csdn.net/m0_74045500/article/details/145082249 浏览次数:0次
河南建设集团有限公司_代驾app软件开发_网站优化提升排名_北京网络seo经理

24. 两两交换链表中的节点

/** @lc app=leetcode.cn id=142 lang=cpp** [142] 环形链表 II*/// @lc code=start
/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:ListNode *detectCycle(ListNode *head) {ListNode* fast=head;ListNode* slow=head;while(fast!=nullptr&&fast->next!=nullptr){slow=slow->next;fast=fast->next->next;if(slow==fast){ListNode* index1=fast;ListNode* index2=head;while(index1!=index2){index1=index1->next;index2=index2->next;}return index2;}}return NULL;}
};
// @lc code=end

19.删除链表的倒数第N个节点

/** @lc app=leetcode.cn id=19 lang=cpp** [19] 删除链表的倒数第 N 个结点*/// @lc code=start
/*** 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* removeNthFromEnd(ListNode* head, int n) {ListNode* dhead=new ListNode(0);dhead->next=head;ListNode* slow=dhead;ListNode* fast=dhead;while(n--&&fast!=nullptr){fast=fast->next;}fast=fast->next;while(fast!=nullptr){fast=fast->next;slow=slow->next;}slow->next=slow->next->next;return dhead->next;}
};
// @lc code=end

面试题 02.07. 链表相交

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {ListNode* curA=headA;ListNode* curB=headB;int lena=0,lenb=0;while(curA!=nullptr){lena++;curA=curA->next;}while(curB!=nullptr){lenb++;curB=curB->next;}curA=headA;curB=headB;if(lenb>lena){swap(lena,lenb);swap(curA,curB);}int gap=lena-lenb;while(gap--){curA=curA->next;}while (curA!=nullptr){if(curA==curB)return curA;curA=curA->next;curB=curB->next;}return NULL;}
};

142.环形链表II

/** @lc app=leetcode.cn id=142 lang=cpp** [142] 环形链表 II*/// @lc code=start
/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:ListNode *detectCycle(ListNode *head) {ListNode* fast=head;ListNode* slow=head;while(fast!=nullptr&&fast->next!=nullptr){slow=slow->next;fast=fast->next->next;if(slow==fast){ListNode* index1=fast;ListNode* index2=head;while(index1!=index2){index1=index1->next;index2=index2->next;}return index2;}}return NULL;}
};
// @lc code=end

链表题一定要多画图模拟过程

关键字:河南建设集团有限公司_代驾app软件开发_网站优化提升排名_北京网络seo经理

版权声明:

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

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

责任编辑: