行业知识
【leetcode】相交链表
2026-11-02 07:15:14
来源:互联网转载
大家好,���是苏貝,本篇博客带大家刷题,如果你觉得我写的还不错的话,可以给我一个赞吗,感谢❤️
点击查看题目
思路:
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) { struct ListNode *cur1=headA; struct ListNode *cur2=headB; int len1=1; int len2=1; //1.找到两个链表的最后一个节点 while(cur1->next) { cur1=cur1->next; len1++; } while(cur2->next) { cur2=cur2->next; len2++; } //2.比较这两个最后节点的地址,如果相同即存在相交节点,不同则不存在 if(cur1!=cur2) { return NULL; } //3.让长的链表先走差距步 struct ListNode *LongList=headA; struct ListNode *ShortList=headB; if(len1 LongList=headB; ShortList=headA; } int len=abs(len1-len2);//求绝对值 while(len--) { LongList=LongList-next; } //一起走 while(LongList!=ShortList) { LongList=LongList->next; ShortList=ShortList->next; } return LongList;} 好了,那么本篇博客就到此结束了,如果你觉得本篇博客对你有些帮助,可以给个大大的赞吗,感谢看到这里,我们下篇博客见❤️
上一篇:渗透干货|史上最全一句话木马
下一篇:怎么重启奥创服务器?