方法一:哈希
时间复杂度O(N+M+2K),空间复杂度O(N)
方法二:双指针
时间复杂度O(2N+2M+K),空间复杂度O(1)
class Solution {
public:
unordered_set<ListNode*> nodes;
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode* p1=headA,*p2=headB;
while(p1!=p2){
p1 = !p1?headB:p1->next;
p2 = !p2?headA:p2->next;
}
return p1;
}
};