判断两个单链表是否相交

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

代码如下:

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

猜你喜欢

转载自www.cnblogs.com/du001011/p/10621874.html