/**
* 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) {
int lenA=0;
int lenB=0;
ListNode* curA=headA;
ListNode* curB=headB;;
while(curA!=NULL){
lenA++;
curA=curA->next;
}
while(curB!=NULL){
lenB++;
curB=curB->next;
}
curA=headA;
curB=headB;
if(lenB>lenA){
swap(lenB,lenA);
swap(curA,curB);
}
int Dv=lenA-lenB;
for(int i=0;i<Dv;i++){
curA=curA->next;
}
while(curA!=NULL){
if(curA==curB){
return curA;
}else{
curA=curA->next;
curB=curB->next;
}
}
return NULL;
}
};
leetcode 面试题02.07 链表相交
最新推荐文章于 2025-11-24 16:32:05 发布
610

被折叠的 条评论
为什么被折叠?



