-
Notifications
You must be signed in to change notification settings - Fork 0
/
Intersection of Two Linked Lists.cpp
68 lines (64 loc) · 1.52 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<iostream>
using namespace std;
struct ListNode{
int val;
ListNode* next;
ListNode(int x):val(x),next(NULL){}
};
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
int cont1=0,cont2=0;
ListNode* tmp1=headA;
ListNode* tmp2=headB;
if (headA==NULL||headB==NULL)
return NULL;
while (tmp1!=NULL)
{
cont1++;
tmp1=tmp1->next;
}
while (tmp2!=NULL)
{
cont2++;
tmp2=tmp2->next;
}
if (cont1<cont2)
{
int cont=cont1;
cont1=cont2;
cont2=cont;
tmp1=headB;
tmp2=headA;
}
else
{
tmp1=headA;
tmp2=headB;
}
for (int i=0;i<cont1-cont2;i++)
{
tmp1=tmp1->next;
}
for (int i=0;i<cont2;i++)
{
if (tmp1==tmp2)
return tmp1;
tmp1=tmp1->next;
tmp2=tmp2->next;
}
return NULL;
}
int main()
{
ListNode* headA=new ListNode(1);
headA->next=new ListNode(2);
headA->next->next=new ListNode(3);
ListNode* headB=new ListNode(4);
headB->next=new ListNode(5);
headB->next->next=new ListNode(6);
headB->next->next->next=headA->next->next;
headA->next->next->next=new ListNode(7);cout<<9;
headA->next->next->next->next=new ListNode(8);
ListNode* tmp=getIntersectionNode(headA,headB);
cout<<tmp->val;
return 0;
}