题目:
Write a program to find the node at which the intersection of two singly linked lists begins.
For example, the following two linked lists:
A: a1 → a2↘c1 → c2 → c3↗
B: b1 → b2 → b3
判断两个链表是否相交,表1和表2都按顺序往下走,若表1走完从2继续走,若表2走完从1继续走,然后两个经过(m+n)次总能找到相交的结点。
class Solution { public:ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {ListNode *ind1 = headA, *ind2 = headB;while(ind1 != ind2){ind1 = ind1?ind1->next:headB;ind2 = ind2?ind2->next:headA;}return ind1;} };