力扣【160】相交链表

题目:

编写一个程序,找到两个单链表相交的起始节点。

如下面的两个链表

在节点 c1 开始相交。

题解:

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode tem2 = headB;
        while (headA != null) {
            while (headB != null) {
                if (headA == headB) {
                    return headB;
                }
                headB = headB.next;
            }
            headB = tem2;
            headA = headA.next;
        }
        return null;
    }
}

猜你喜欢

转载自blog.csdn.net/qq1922631820/article/details/111057083