-
Notifications
You must be signed in to change notification settings - Fork 7
/
Main.java
55 lines (48 loc) · 1.42 KB
/
Main.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
package com.company;
import java.util.HashSet;
class ListNode {
int val;
ListNode next;
ListNode(int x, ListNode next) {
val = x;
this.next = next;
}
}
public class Main {
public static void main(String[] args) {
ListNode headA = generate(new int[]{2, 6, 4});
ListNode headB = generate(new int[]{1, 5});
getIntersectionNode_NSpace(headA, headB);
}
public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
HashSet<ListNode> nodes = new HashSet<>();
while (headA != null){
nodes.add(headA);
headA = headA.next;
}
while (headB != null){
if(nodes.contains(headB))
return headB;
headB = headB.next;
}
return null;
}
public static ListNode getIntersectionNode_NSpace(ListNode headA, ListNode headB) {
if(headA == null || headB == null) return null;
ListNode p1 = headA, p2 = headB;
while (p1 != p2){
p1 = p1.next;
p2 = p2.next;
if(p1 == p2) return p1;
if(p1 == null) p1 = headB;
if(p2 == null) p2 = headA;
}
return p1;
}
public static ListNode generate(int[] nums){
ListNode res = null;
for (int i = nums.length - 1; i >= 0; i--)
res = new ListNode(nums[i], res);
return res;
}
}