LeetCode:160 相交链表(本来用的哈希表,发现题解有更好的解法,学习了一下)

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;
    }
}

 

posted @ 2020-10-05 10:20  dlooooo  阅读(69)  评论(0编辑  收藏  举报