方法一:利用C++的set容器求交集
思路
- 将链表A的元素放入到set集合中
- 遍历链表B,如果链表B的某个结点在set集合中,返回该节点
代码实现
struct ListNode
{
int val;
ListNode* next;
ListNode(int x) :val(x), next(NULL) {
}
};
class Solution1//用set容器求解
{
public:
static ListNode* getIntersectionNode(ListNode* headA,ListNode* headB) {
set<ListNode*> node_set;
while (headA)
{
node_set.insert(headA);