关于两个链表的交点的小结

本文探讨了两个链表相交的六种情况,包括无环不相交、无环相交、有环不相交、有环相交等,并详细分析了如何判断链表是否相交以及交点位置。通过两重循环、判断环的存在以及求解切入点来解决问题。最后,提供了实现功能的函数代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对于链表可分为有环和无环,对于两个链表的关系可分为相交和不相交
那么两个链表的相交分为哪几种情况呢?
可以简单的分为六种情况
1.两个无环且不相交
2.两个无环相交
3.一个有环不相交
4.两个有环不相交
5.两个有环相交,交点不在环上
6.两个有环相交,交点在环上
下面画一张图就可以清楚的看到这几种情况
这里写图片描述
这六种情况又可以分为三种情形考虑
第一种情形
把第一种和第二种情况放在一起分析(两个链表都无环)。用两重循环,若两个都遍历到尾,都没有结点相同,则不相交。

Node* meetNodeNoRing(Node* list1, Node* list2)//无环
{
    assert(list1&&list2);
    Node* cur = list2;
    while (list1)
    {
        list2 = cur;
        while (list2)
        {
            if (list1 == list2)
                return list1;
            else
                list2 = list2->next;
        }
        list1 = list1->next;
    }
    return NULL;
}

第二种情形
第三种,只要证明出一个有环,一个无环,则两个必不相交

//判断一个链表是否有环
bool findRing(Node* list)//用快慢指针
{
    if (list == NULL)
        return false;
    Node* fast = list;
    Node* slow = list;
    while (fast&& fast->next)
    {
        Node* next = fast->next;
        slow = slow->next;
        fast = next->next;
        if (fast == slow)
            return true;
    }
    return false;

}

第三种情形
第四种,第五种,第六种放在一起分析
先判断出两个都有环,求出环的切入点,若两个链表的切入点相同,则两个链表相交,且交点不在环上,然后求出链表的头到切入点的距离,哪个距离大就先往后移动两个距离的差值,再两个链表同时往后移动,相同的结点就是交点。若切入点不同,一个链表的切入点不动,另一个往后移动,若两个会相遇,则相交,且整个链表都是交点。对于都有环,除去上述两种情况,只剩下不相交这种情况。

Node* meetNodeRing(Node* list1, Node* list2)//有环,只有两个都有环时,才可能相交
{
    assert(list1&&list2);
    if (!findRing(list1) && findRing(list2))//一个有环,一个无环,两个必不相交
        //1没有环,2有环
        return NULL;
    else if (findRing(list1) && !findRing(list2))
        //1有环,2没有环
        return NULL;
    Node* meetNode1 = MeetNode(list1);
    Node* meetNode2 = MeetNode(list2);
    Node *Enter1 = RingEnter(list1, meetNode1);
    Node *Enter2 = RingEnter(list2, meetNode2);
    if (findRing(list1) && findRing(list2))
    {
        if (Enter1 == Enter2)//两个环相交,入口点相同,所以交点不在环上
        {
            int count1 = HeadToEnter(list1, RingEnter(list1, meetNode1));//求出链表头到切入点的距离
            int count2 = HeadToEnter(list2, RingEnter(list1, meetNode2));
            if (count1 > count2)
            {
                int count = count1 - count2;
                while (count--)
                {
                    list1 = list1->next;
                }
                while (list1 != list2)
                {
                    list1 = list1->next;
                    list2 = list2->next;
                }
            }
            else if (count1 < count2)
            {
                int count = count2 - count1;
                while (count--)
                {
                    list2 = list2->next;
                }
                while (list1 != list2)
                {
                    list1 = list1->next;
                    list2 = list2->next;
                }
            }
            else
            {
                while (list1 != list2)
                {
                    list1 = list1->next;
                    list2 = list2->next;
                }
            }
            return list1;
        }
        else//入口点不同,可能相交,也可能不相交
        {
            Node* next = Enter1->next;
            while (Enter1 != next)
            {
                if (next == Enter2)//相等说明,两个链的入口点都在环上,整个环都是交点
                    return next;
                next = next->next;
            }
            return NULL;//两个链有环 不相交 
        }
    }
    return NULL;
}

附上运行所需的整体的函数代码

Node*  MeetNode(Node* list)//求相遇点
{
    if (list == NULL)
        return NULL;
    Node* fast = list;
    Node* slow = list;

    while (fast&& fast->next)
    {
        Node* next = fast->next;
        slow = slow->next;
        fast = next->next;
        next = fast->next;
        if (fast == slow)
            return fast;
    }
    return NULL;
}

bool findRing(Node* list)//用快慢指针,判断有没有环
{
    if (list == NULL)
        return false;
    Node* fast = list;
    Node* slow = list;
    while (fast&& fast->next)
    {
        Node* next = fast->next;
        slow = slow->next;
        fast = next->next;
        if (fast == slow)
            return true;
    }
    return false;

}
#include<assert.h>
Node* RingEnter(Node*list, Node* meetNode)//求环的切入点
//时间复杂度为O(N)
{
    assert(list&&meetNode);
    while (list != meetNode)
    {
        list = list->next;
        meetNode = meetNode->next;
    }
    return meetNode;
}

void Printf(Node* list)
{
    if (list == NULL)
        return;
    while (list)
    {
        cout << list->data << " ";
        list=list->next;
    }
    cout << endl;
}

Node* meetNodeNoRing(Node* list1, Node* list2)//无环
{
    assert(list1&&list2);
    Node* cur = list2;
    while (list1)
    {
        list2 = cur;
        while (list2)
        {
            if (list1 == list2)
                return list1;
            else
                list2 = list2->next;
        }
        list1 = list1->next;
    }
    return NULL;
}
int HeadToEnter(Node* list, Node* enter)//链表头到环入口的距离
{
    assert(list&&enter);
    int count = 0;
    while (list != enter)
    {
        count++;
        list = list->next;
    }
    return count;
}
Node* meetNodeRing(Node* list1, Node* list2)//有环,只有两个都有环时,才可能相交
{
    assert(list1&&list2);
    if (!findRing(list1) && findRing(list2))//一个有环,一个无环,两个必不相交
        //1没有环,2有环
        return NULL;
    else if (findRing(list1) && !findRing(list2))
        //1有环,2没有环
        return NULL;
    Node* meetNode1 = MeetNode(list1);
    Node* meetNode2 = MeetNode(list2);
    Node *Enter1 = RingEnter(list1, meetNode1);
    Node *Enter2 = RingEnter(list2, meetNode2);
    if (findRing(list1) && findRing(list2))
    {
        if (Enter1 == Enter2)//两个环相交,入口点相同,所以交点不在环上
        {
            int count1 = HeadToEnter(list1, RingEnter(list1, meetNode1));
            int count2 = HeadToEnter(list2, RingEnter(list1, meetNode2));
            if (count1 > count2)
            {
                int count = count1 - count2;
                while (count--)
                {
                    list1 = list1->next;
                }
                while (list1 != list2)
                {
                    list1 = list1->next;
                    list2 = list2->next;
                }
            }
            else if (count1 < count2)
            {
                int count = count2 - count1;
                while (count--)
                {
                    list2 = list2->next;
                }
                while (list1 != list2)
                {
                    list1 = list1->next;
                    list2 = list2->next;
                }
            }
            else
            {
                while (list1 != list2)
                {
                    list1 = list1->next;
                    list2 = list2->next;
                }
            }
            return list1;
        }
        else//入口点不同,可能相交,也可能不相交
        {
            Node* next = Enter1->next;
            while (Enter1 != next)
            {
                if (next == Enter2)//相等说明,两个链的入口点都在环上,整个环都是交点
                    return next;
                next = next->next;
            }
            return NULL;//两个链有环 不相交 
        }
    }
    return NULL;
}

void deleteNoTail(Node* list,Node* cur)
{
    assert(list&&cur);

    while (list&&list->next != cur)
    {
        list = list->next;
    }
    list->next = cur->next;
}

Node* find(Node *list,int x)
{
    while (list->data != x)
        list = list->next;
    return list;
}
void PrintTailToHead(Node* list)
{
    stack<Node*> s;
    while (list)
    {
        s.push(list);
        list=list->next;
    }
    while (!s.empty())
    {
        cout << s.top()->data << " ";
        s.pop();
    }
    cout << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值