-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProblem-1.java
73 lines (55 loc) · 1.51 KB
/
Problem-1.java
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
69
70
71
72
73
/**
* @author AkashGoyal
*/
/**
--------------------- Problem----------->> Intersection Point in Y Shapped Linked Lists
Problem Link :- https://practice.geeksforgeeks.org/problems/intersection-point-in-y-shapped-linked-lists/1
*/
class Intersect
{
//Function to find intersection point in Y shaped Linked Lists.
int intersectPoint(Node head1, Node head2)
{
// code here
int count1=0;
int count2=0;
Node node1=head1;
Node node2=head2;
while(head1.next!=null)
{
head1=head1.next;
count1++;
}
while(head2.next!=null)
{
head2=head2.next;
count2++;
}
head1=node1;
head2=node2;
int diff=Math.abs(count1-count2);
if(count1>count2)
{
while(diff>0)
{
head1=head1.next;
diff--;
}
}
else
{
while(diff>0)
{
head2=head2.next;
diff--;
}
}
int data=0;
while(head1.next!=null && head2.next!=null && head1!=head2)
{
head1=head1.next;
head2=head2.next;
}
return head1.data;
}
}