-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathSolution.java
More file actions
43 lines (36 loc) · 794 Bytes
/
Solution.java
File metadata and controls
43 lines (36 loc) · 794 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
34
35
36
37
38
39
40
41
42
43
/*
Find merge point of two linked lists
head pointer input could be NULL as well for empty list
Node is defined as
class Node {
int data;
Node next;
}
*/
int lengthNode(Node n) {
int counter = 0;
while (n != null) {
counter++;
n = n.next;
}
return counter;
}
int FindMergeNode(Node headA, Node headB) {
int lA = lengthNode(headA);
int lB = lengthNode(headB);
int d = Math.abs(lA - lB);
if (lA > lB) {
for (int i = 0; i < d; i++) {
headA = headA.next;
}
} else if (lA < lB) {
for (int i = 0; i < d; i++) {
headB = headB.next;
}
}
while (headA.data != headB.data) {
headA = headA.next;
headB = headB.next;
}
return headA.data;
}