[leetCode]面试题 02.07. 链表相交

双指针
通过交换指针位置来使指针在循环过程中达到相同的位置
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) return null;
        ListNode pA = headA;
        ListNode pB = headB;
        while (true) {
            if (pA == null && pB == null) return null;
            if (pA == null) {
                pA = headB;
            }
            if (pB == null) {
                pB = headA;
            }
            if (pA == pB) return pB;
            pA = pA.next;
            pB = pB.next;
        }
    }
}

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号