-
Notifications
You must be signed in to change notification settings - Fork 0
/
Intersection of Two Linked Lists.cpp
51 lines (51 loc) · 1.19 KB
/
Intersection of Two Linked Lists.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* 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) {
if(headA==NULL||headB==NULL)
{
return NULL;
}
ListNode* aNode=headA;
ListNode* bNode=headB;
int aCount=0;
int bCount=0;
int a_b=0;
while(aNode!=NULL)//计算A链表的长度
{
aCount++;
aNode=aNode->next;
}
while(bNode!=NULL)//计算B链表的长度
{
bCount++;
bNode=bNode->next;
}
aNode=headA;
bNode=headB;
int i=0;
while(aCount-bCount>i)//A长,A就先走a-b步
{
i++;
aNode=aNode->next;
}
while(bCount-aCount>i)//B长,B就先走b-a步
{
i++;
bNode=bNode->next;
}
while(aNode!=NULL&bNode!=NULL&&aNode!=bNode)//剩下的长度一样长,就一起走
{
aNode=aNode->next;
bNode=bNode->next;
}
return aNode;
}
};