forked from architsingla13/InterviewBit-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntersection.java
More file actions
64 lines (56 loc) · 1.21 KB
/
Intersection.java
File metadata and controls
64 lines (56 loc) · 1.21 KB
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
package LinkedLists;
/**
* Author - archit.s
* Date - 17/10/18
* Time - 12:18 PM
*/
class ListNode {
public int val;
public ListNode next;
ListNode(int x) { val = x; next = null; }
}
public class Intersection {
public ListNode getIntersectionNode(ListNode a, ListNode b) {
int l1 = 0;
int l2 = 0;
if(a == null || b == null){
return null;
}
ListNode temp = a;
while(temp!=null){
l1++;
temp = temp.next;
}
temp = b;
while(temp != null){
l2++;
temp = temp.next;
}
if(l1<l2){
int count = 0;
while(count < (l2-l1)){
b = b.next;
count++;
}
}
else{
int count = 0;
while(count < (l1-l2)){
a = a.next;
count++;
}
}
ListNode t1 = a;
ListNode t2 = b;
while(t1!= null && t2 != null){
if(t1 == t2){
return t1;
}
else{
t1 = t1.next;
t2 = t2.next;
}
}
return null;
}
}