企业网站管理系统设计报告苏州网站优化公司
leetcode Hot 100系列
文章目录
- 一、核心操作
- 二、外层配合操作
- 三、核心模式代码
- 总结
一、核心操作
- 先用curA和curB统计两个链表的长度,统计完之后,
cur都要归位!
- 如果A<B,则交换curA和curB,以及两个链表的长度
- 将curA先移动A与B的差值,然后两个链表同步向后搜索,如果有相等的则是找到了,如果遍历到最后了都没找到就返回nullptr
提示:小白个人理解,如有错误敬请谅解!
二、外层配合操作
- 无
三、核心模式代码
代码如下:
class Solution {
public:ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {ListNode* curA=headA;ListNode* curB=headB;int countA=0;int countB=0;while (curA!=nullptr){countA++;curA=curA->next;}while (curB!=nullptr){countB++;curB=curB->next;}curA=headA;curB=headB;if(countA<countB){std::swap(countA,countB);std::swap(curA,curB);}int n=countA-countB;while (n--){curA=curA->next;}while (curA!=nullptr && curB!=nullptr){curA=curA->next;curB=curB->next;if(curA==curB)return curA;}return nullptr;}
};
总结
- cur要归位,交换cur和count