单链表相交判断

57 篇文章 1 订阅
11 篇文章 0 订阅

给定两个单链表的头节点head1和head2,如何判断两个链表是否相交?相交的话返回true,不想交的话返回false。

给定两个链表的头结点head1head2(注意,另外两个参数adjust0adjust1用于调整数据,与本题求解无关)。请返回一个bool值代表它们是否相交。

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};*/
class ChkIntersection {
public:
    ListNode* chkLoop(ListNode* head) //判断链表是否有环,若有环,返回刚开始进环的结点
    {
        if(head==NULL||head->next==NULL)
            return NULL;
        ListNode *fast,*slow;
        fast=slow=head;
        while(fast!=NULL&&fast->next!=NULL)
        {
            slow=slow->next;
            fast=fast->next->next;
            if(fast==slow)
                break;
        }
        if(fast==slow)
        {
            fast=head;
            while(fast!=slow)
            {
                fast=fast->next;
                slow=slow->next;
            }
            return slow;
        }
        else
        {
            return NULL;
        }
    }
    bool chkInter(ListNode* head1, ListNode* head2, int adjust0, int adjust1) {
        // write code here
        ListNode *start1,*start2;
        start1=chkLoop(head1);
        start2=chkLoop(head2);
        if(start1==NULL&&start2==NULL)//无环单链表相交判断
        {
            ListNode* temp;
            temp=head1;
            int lena=0,lenb=0;
            while(temp)
            {
                temp=temp->next;
                lena++;
            }
            temp=head2;
            while(temp)
            {
                temp=temp->next;
                lenb++;
            }
            if(lena>lenb)
            {
                for(int i=0;i<lena-lenb;i++)
                {
                    head1=head1->next;
                }
                while(head1)
                {
                    if(head1==head2)
                        return true;
                    head1=head1->next;
                    head2=head2->next;
                }
                return false;
            }
            else
            {
                for(int i=0;i<lenb-lena;i++)
                {
                    head2=head2->next;
                }
                while(head1)
                {
                    if(head1==head2)
                        return true;
                    head1=head1->next;
                    head2=head2->next;
                }
                return false;
            }
        }
        else if(start1!=NULL&&start2!=NULL)//有环单链表相交判断
        {
            if(start1==start2)
                return true;
            ListNode *p;
            p=start1;
            while(true)
            {
                p=p->next;
                if(p==start1)
                    return false;
                if(p==start2)
                    return true;
            }
        }
        else{
            return false;
        }
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值