😈博客主页:🐼大家好我叫张同学🐼
💖 欢迎点赞 👍 收藏 💗留言 📝 欢迎讨论! 👀
🎵本文由 【大家好我叫张同学】 原创,首发于 CSDN 🌟🌟🌟
✨精品专栏(不定时更新) 【数据结构+算法】 【做题笔记】【C语言编程学习】
☀️ 精品文章推荐
【C语言进阶学习笔记】三、字符串函数详解(1)(爆肝吐血整理,建议收藏!!!)
【C语言基础学习笔记】+【C语言进阶学习笔记】总结篇(坚持才有收获!)
图示两个链表在节点 c1 开始相交:


原题链接(点击跳转)
理解相交:不是
指A
链表中有和B
链表中val
值相同的结点,而是
指这个两个链表中存在相同
的结点
,如果我们通过指针
去遍历A
、B
两个链表,能够在A
、B
中找到相同地址
的结点
,那A、B相交
,否则
就是不相交
。

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
struct ListNode *curA = headA,*curB = headB;
while(curA){
curB = headB;
while(curB){
if(curB == curA)
return curB;
curB = curB->next;
}
curA = curA->next;
}
return NULL;
}

这种方式的时间复杂度为O(n^2),空间复杂度是O(1),那能不能想办法改进,降低时间复杂度呢?最好是能实现O(m+n) time,O(1) memory。

其实要判断两个链表是否相交很简单,我们可以直接判断两个链表的最后一个结点
是否相同,若相同则肯定相交,否则就不相交。
但比较麻烦的是我们还需要返回相交的结点,为了实现这个要求,我们可以再遍历A、B
两个链表找最后一个结点的时候求A、B的
长度lenA
、lenB
。之后让A、B
链表中较长的
先走 step = | lenA - lenB |
(| |表示绝对值)步。然后两个再一起往下走
,遇到的第一个相同的结点即为相交结点
。
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
if(headA == NULL || headB == NULL)
return NULL;
struct ListNode *curA = headA,*curB = headB;
int lenA = 1,lenB = 1;
while(curA && curA->next){
lenA++;
curA = curA->next;
}
while(curB && curB->next){
lenB++;
curB = curB->next;
}
curA = headA;
curB = headB;
if(lenA > lenB){
int step = lenA - lenB;
while(step--){
curA = curA->next;
}
}
else if(lenB > lenA){
int step = lenB - lenA;
while(step--){
curB = curB->next;
}
}
while(curA != curB){
curA = curA->next;
curB = curB->next;
}
return curA;
}

除了刚刚说的思路外,我们也可不用求A、B链表的长度来实现返回相交结点的目的,不过这种方式需要一点点的数学技巧。

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
struct ListNode *curA = headA,*curB = headB;
while(curA != curB){
curA = curA == NULL ? headB : curA->next;
curB = curB == NULL ? headA : curB->next;
}
return curA;
}
