链表-返回两个链表的第一个公共节点

//返回两个链表的第一个公共节点
public Node FindFirstCommonNode(Node head1, Node head2) {
    Node p1 = head1;
    Node p2 = head2;
    while (p1 != p2){
        p1 = (p1 != null ? p1.next : head2);
        p2 = (p2 != null ? p2.next : head1);
    }
    return p1;
}

猜你喜欢

转载自blog.csdn.net/weixin_42146769/article/details/88389482
今日推荐