Intersection of linked list
Find Intersection of two linked list
/**
* Definition for singly-linked list.
* Definition for singly-linked list.
* struct ListNode {
* struct ListNode {
* int val;
* int val;
* ListNode *next;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* ListNode(int x) : val(x), next(NULL) {}
* };
* };
*/
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode* p1 = headA;
ListNode* p2 = headB;
while (p1 != p2) {
p1 = p1 ? p1->next : headA;
p2 = p2 ? p2->next : headB;
}
return p1;
}
};