首页 > 技术文章 > 判断两个单链表是否相交

du001011 2019-03-29 15:57 原文

思想:两链表如果相交,则两链表最后一个元素相等

代码如下:

public static boolean isIntersect(ListNode l1,ListNode l2) {
        if(l1 == null || l2 == null) return false;
        while(l1.next!=null) {
            l1 = l1.next;
        }
        while(l2.next != null) {
            l2 = l2.next;
        }
        if(l1 == l2)
            return true;
        return false;
}

 

推荐阅读