<LinkedList> 160

160. Intersection of Two Linked Lists

分别从AB循环两次。如果第一次没循环到,第二次就会在节点相遇。

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null || headB == null) return null;
        
        ListNode a = headA;
        ListNode b = headB;
        
        while(a != b){
            a = a == null ? headB : a.next;
            b = b == null ? headA : b.next;
        }
        return a;
    }
}

21. Merge Two Sorted Lists

猜你喜欢

转载自www.cnblogs.com/Afei-1123/p/11918925.html