-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Solution.java
37 lines (31 loc) · 842 Bytes
/
Solution.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
// github.com/RodneyShag
/*
Node is defined as
class Node {
int data;
Node next;
}
*/
// Create a pointer that iterates through a list. When it's at the end
// of the list, have it jump to the beginning of the other list. Create
// 2 of these pointers, pointing to 2 different list heads. The pointers
// will collide at the merge point after 1 or 2 passes.
// Time Complexity: O(n + m)
// Space Complexity: O(1)
int FindMergeNode(Node headA, Node headB) {
Node currA = headA;
Node currB = headB;
while (currA != currB) {
if (currA.next == null) {
currA = headB;
} else {
currA = currA.next;
}
if (currB.next == null) {
currB = headA;
} else {
currB = currB.next;
}
}
return currA.data;
}