这个题我们可以用双指针来做,若这两个链表相交,那跑一个循环两指针必相遇
/**
* 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) {
ListNode *node1 = headA;
ListNode *node2 = headB;
while(node1!=node2)
{
node1 = node1==NULL?headB:node1->next;
node2 = node2==NULL?headA:node2->next;
}
return node1;
}
};