# 力扣160. 相交链表 哈希、双指针、差值法、栈四种花式解法

2021-06-07 20:59:32音视频开发人已围观

``````输入：intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3

``````

``````输入：intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1

``````

``````输入：intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2

``````

listA 中节点数目为 m
listB 中节点数目为 n
0 <= m, n <= 3 * 104
1 <= Node.val <= 105
0 <= skipA <= m
0 <= skipB <= n

``````/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
set<ListNode* > myset;
while(a) {
myset.insert(a);
a = a->next;
}
while (b) {
if (myset.find(b) != myset.end()) return b;
else b = b->next;
}
return nullptr;
}
};
``````

why？

``````/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
return nullptr;
}
while (pA != pB) {
pA = pA == nullptr ? headB : pA->next;
pB = pB == nullptr ? headA : pB->next;
}
return pA;
}
};
``````

Tags：链表 哈希表 指针 数据结构 leetcode

## 文章评论

 留言与评论（共有 0 条评论）

 验证码：

## 站点信息

• 文章统计247968篇文章
• 浏览统计18356次浏览
• 评论统计1个评论
• 标签管理标签云
• 统计数据：统计代码
• 微信公众号：扫描二维码，关注我们