listA 中节点数目为 m
listB 中节点数目为 n
1 <= m, n <= 3 * 104
1 <= Node.val <= 105
0 <= skipA <= m
0 <= skipB <= n
如果 listA 和 listB 没有交点,intersectVal 为 0
如果 listA 和 listB 有交点,intersectVal == listA[skipA] == listB[skipB]
一、解题关键词
二、解题报告
1.思路分析
2.时间复杂度
3.代码示例
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/publicclassSolution{publicListNodegetIntersectionNode(ListNode headA,ListNode headB){Set<ListNode> visited =newHashSet<ListNode>();ListNode tmp = headA;while(tmp !=null){
visited.add(tmp);
tmp = tmp.next;}
tmp = headB;while(tmp !=null){if(visited.contains(tmp)){return tmp;}
tmp = tmp.next;}returnnull;}}