-
Notifications
You must be signed in to change notification settings - Fork 8
/
dsa.cpp
66 lines (58 loc) · 1.08 KB
/
dsa.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
/*
Find merge point of two linked lists
Node is defined as
struct Node
{
int data;
Node* next;
}
*/
int getCount(Node* head)
{
Node* current = head;
int count = 0;
while (current != NULL)
{
count++;
current = current->next;
}
return count;
}
int getNode(int d, Node* head1, Node* head2)
{
int i;
Node* current1 = head1;
Node* current2 = head2;
for(i = 0; i < d; i++)
{
if(current1 == NULL)
{ return -1; }
current1 = current1->next;
}
while(current1 != NULL && current2 != NULL)
{
if(current1 == current2)
return current1->data;
current1= current1->next;
current2= current2->next;
}
return -1;
}
int FindMergeNode(Node *headA, Node *headB)
{
// Complete this function
// Do not write the main method.
int c1 = getCount(headA);
int c2 = getCount(headB);
int d;
if(c1 > c2)
{
d = c1 - c2;
return getNode(d, headA, headB);
}
else
{
d = c2 - c1;
return getNode(d, headB, headA);
}
}