forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_160.java
94 lines (80 loc) · 2.55 KB
/
_160.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.fishercoder.solutions;
import com.fishercoder.common.classes.ListNode;
import java.util.HashSet;
import java.util.Set;
public class _160 {
public static class Solution1 {
/**
* Time: O(max(m, n))
* Space: O(1)
*/
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
int lenA = findLen(headA);
int lenB = findLen(headB);
/**align headA and headB to the same starting point and then move together until we find the intersection point*/
while (lenA < lenB) {
headB = headB.next;
lenB--;
}
while (lenB < lenA) {
headA = headA.next;
lenA--;
}
while (headA != headB) {
headA = headA.next;
headB = headB.next;
}
return headA;
}
private int findLen(ListNode head) {
int len = 0;
while (head != null) {
head = head.next;
len++;
}
return len;
}
}
public static class Solution2 {
/**
* Most optimal solution:
* O(m+n) time
* O(1) space
* credit: https://discuss.leetcode.com/topic/28067/java-solution-without-knowing-the-difference-in-len
*/
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) {
return null;
}
ListNode a = headA;
ListNode b = headB;
/**if a and b have different lengths, then it will stop the loop after second iteration*/
while (a != b) {
/**for the first iteration, it'll just reset the pointer to the head of another linkedlist*/
a = a == null ? headB : a.next;
b = b == null ? headA : b.next;
}
return a;
}
}
public static class Solution3 {
/**
* O(m+n) time
* O(Math.max(m, n)) space
*/
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
Set<ListNode> set = new HashSet<>();
while (headA != null) {
set.add(headA);
headA = headA.next;
}
while (headB != null) {
if (set.contains(headB)) {
return headB;
}
headB = headB.next;
}
return null;
}
}
}