题目描述:
示例 1:

示例 2:

示例 3:

思路一:
代码实现:
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
Map<ListNode, Integer> map = new HashMap();
ListNode curOne = headA;
while (curOne != null) {
map.put(curOne, curOne.val);
curOne = curOne.next;
}
ListNode curTwo = headB;
Integer result = null;
while (curTwo != null) {
result = map.get(curTwo);
if (result != null) {
return curTwo;
}
curTwo = curTwo.next;
}
return curTwo;
}
}
思路二:
代码实现:
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode one = headA, two = headB;
if (one == null || two == null) {
return null;
}
while (one != two) {
one = one == null ? headB : one.next;
two = two == null ? headA : two.next;
}
return one;
}
}