leetcode 160. Intersection of Two Linked Lists(两个链表的交点)

Write a program to find the node at which the intersection of two singly linked lists begins.

For example, the following two linked lists:
在这里插入图片描述
让找到两个链表的交叉点

思路:
方法一:
遍历A和B链表,A走到tail时转到B的head,B走到tail时转到A的head
然后必然会走到相同的点
两个链表有交叉点的前提是后面的node必须有一致,如果走到tail还不一致,证明不存在交叉点,返回null

//1ms
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) {
            return null;
        }
        
        ListNode a = headA;
        ListNode b = headB;
        
        while (a != null && b != null) {
            
            if (a == b) {
                return a;
            }
            
            a = a.next;
            b = b.next;
               
            if (a == null && b == null) {
                return null;
            }
            
            if (a == null) {
                a = headB;
            }
            
            if (b == null) {
                b = headA;
            }
        }
        
        return null;
    }

方法二:
快慢指针法,
连接末尾和headA,形成一个环,于是转换为链表的环的问题。
快指针每次走2步,慢指针每次走一步,如果相遇则存在环,有一方走到null则不存在环,返回null(别忘了修改指针把环去掉)。
然后慢指针回到headB(因为A是环里面的),
快慢指针每次都走一步,它们相遇的地方就是环的入口。

由于这个环是自己添加的,所以返回前要把链表恢复到最初的状态。

public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
    ListNode tail = headA;
    ListNode fast = headB;
    ListNode slow = headB;
    
    while(tail.next != null) {
        tail = tail.next;
    }
    tail.next = headA;
    
    while(true) {
        if(fast == null || fast.next == null || slow == null) {
            tail.next = null;
            return null;
        }
        fast = fast.next.next;
        slow = slow.next;
        if(fast == slow) break;
    }
    
    slow = headB;
    while(slow != fast) {
        slow = slow.next;
        fast = fast.next;
    }
    
    tail.next = null;
    return slow;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值