Skip to content

E160.相交链表

two pinters, https://leetcode.cn/problems/intersection-of-two-linked-lists/

思路:一路存地址即可,用时约20min

cpp
class Solution 
{
public:
    ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) 
	{
        map<ListNode*, int> m;
		while (headA != NULL)
		{
            m[headA] = 1;
            headA = headA->next;

		}
		while (headB != NULL)
		{
            if (m.find(headB) != m.end())
                return headB;
            headB = headB->next;
		}
        return NULL;

    }
};