比较慢,期待新的解法;
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if(headA == NULL || headB == NULL)
return NULL;
unordered_set<ListNode*> l;
while(headA !=NULL){
l.insert(headA);
headA = headA->next;
}
while(headB != NULL){
if(l.find(headB) != l.end())
return headB;
headB = headB->next;
}
return NULL;
}
};