Leetcode_160 Intersecting Linked List (Romantic)

topic

image-20210317090226479

Ideas

class ListNode {
    
    
    int val;
    ListNode next;

    ListNode(int x) {
    
    
        val = x;
        next = null;
    }
}

/*
文艺版解释:听闻远方有你,动身跋涉千里,我吹过你吹过的风,这算不算相拥?我走过你走过的路,这算不算相逢?
走到尽头见不到你,于是走过你来时的路,等到相遇时才发现,你也走过我来时的路

浪漫的算法
2021/3/17
11:59
*/

class Solution {
    
    
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
    
    

        while (headA == null || headB == null) return null;
        
        
        ListNode pa = headA;
        ListNode pb = headB;
        while (pa != pb) {
    
    
            pa = pa == null ? headB : pa.next;
            pb = pb == null ? headA : pb.next;
        }
        return pa;
    }
}

Thinking

Guess you like

Origin blog.csdn.net/AC_872767407/article/details/114955443