面试题 02.07. 链表相交

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode preA = headA;
        ListNode preB = headB;

        while(preA != preB){
            preA = preA != null ? preA.next: headB;
            preB = preB != null ? preB.next: headA;
        }
        return preA;
    }
}

你可能感兴趣的:(LeetCode算法题,算法)