-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path链表相交
33 lines (33 loc) · 769 Bytes
/
链表相交
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
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
int num1=0;
ListNode* node=headA;
while(node!=NULL){
node=node->next;
num1++;
}
node=headB;
int num2=0;
while(node!=NULL){
node=node->next;
num2++;
}
if(num1>num2){
for(int i=0;i<num1-num2;i++){
headA=headA->next;
}
}
else{
for(int i=0;i<num2-num1;i++){
headB=headB->next;
}
}
while(headA!=NULL){
if(headA==headB) return headA;
headA=headA->next;
headB=headB->next;
}
return NULL;
}
};