/
Solution160.java
39 lines (35 loc) · 1.01 KB
/
Solution160.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
package com.zhengyao.algorithm.list.doublepointer;
import com.zhengyao.algorithm.list.ListNode;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
/**
* @author : zhengyao3@郑瑶
* @date : 2020/3/29 18:00
* @Description: 相交链表
*/
public class Solution160 {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode tempA = headA;
ListNode tempB = headB;
while(tempA != tempB){
tempA = tempA == null ? headB : tempA.next;
tempB = tempB == null ? headA : tempB.next;
}
return tempA;
}
public ListNode getIntersectionNode2(ListNode headA, ListNode headB) {
Set<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;
}
}