0160.cpp (512B)
1 class Solution { 2 public: 3 ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { 4 if (!headA || !headB) return nullptr; 5 6 ListNode *res = nullptr; 7 8 for (ListNode *p = headA; p; p = p->next) 9 p->val = -p->val; 10 11 for (ListNode *p = headB; p; p = p->next) 12 if (p->val < 0) { 13 res = p; 14 break; 15 } 16 17 for (ListNode *p = headA; p; p = p->next) 18 p->val = -p->val; 19 20 return res; 21 } 22 };