Skip to content

Instantly share code, notes, and snippets.

@byam
Created February 21, 2022 15:29
Show Gist options
  • Save byam/9c46dccfdc90ee233565542deaad834e to your computer and use it in GitHub Desktop.
Save byam/9c46dccfdc90ee233565542deaad834e to your computer and use it in GitHub Desktop.
/**
* 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 or !headB) return NULL;
auto pA = headA;
auto pB = headB;
while(pA != pB){
// move both 1 step
pA = pA->next;
pB = pB->next;
// meeth same node
if(pA == pB) break;
// restart different list
if(pA == NULL) pA = headB;
if(pB == NULL) pB = headA;
}
return pA;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment