力扣 160 相交链表 快慢指针 双指针
[题目链接](https://leetcode-cn.com/problems/intersection-of-two-linked-lists/)
思路一
// 思路一,hashmap记录hashcode,没有重写的hashcode都与内存地址有关,所以这样是可以的。
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
HashMap<Integer, Boolean> map = new HashMap<>();
while (headA != null) {
map.put(headA.hashCode(), true);
headA = headA.next;
}
while (headB != null) {
if (map.get(headB.hashCode()) == null) {
map.put(headB.hashCode(), true);
}
else {
return headB;
}
headB = headB.next;
}
return null;
}
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode pA = headA;
ListNode pB = headB;
while (pA != pB) {
pA = pA == null ? headA : pA.next;
pB = pB == null ? headA : pB.next;
pB = pB == null ? headA : pB.next;
}
return pA;
}