数据结构之单链表常见面试题2

判断两链表是否相交,返回交点(考虑带环)

linklist *linklist_iscrossplus(linklist *head1,linklist *head2)
{
    if(head1 == NULL)
    {
        return NULL;
    }
    if(head2 == NULL)
    {
        return NULL;
    }
    else
    {
        linklist *cur1 = linklist_getenter(head1);
        linklist *cur2 = linklist_getenter(head2);
        if(cur1 == NULL && cur2 == NULL)
        {
            linklist_iscross(head1,head2);
        }
        else if((cur1 != NULL && cur2 == NULL) || (cur2 != NULL && cur1 == NULL))
        {
            return NULL;//这种情况不存在
        }
        else
        {
            if(cur1 == cur2)
            {
                //有交点,且交点在环外
                linklist *pos1 = head1;
                linklist *pos2 = head2;
                int len1 = 0;
                int len2 = 0;
                while(pos1 != cur1)
                {
                    pos1 = pos1->next;
                    len1++;
                }
                while(po2 != cur1)
                {
                    pos2 = pos2->next;
                    len2++;
                }
                pos1 = head1;
                pos2 = head2;
                int len = 0;
                if(len1-len2)
                {
                    len = len1-len2;
                    while(len)
                    {
                        pos1 = pos1->next;
                        len--;
                    }
                }
                else
                {
                    len = len2-len1;
                    while(len)
                    {
                        pos2 = pos2->next;
                        len--;
                    }
                }
                while(pos1 != pos2)
                {
                    pos1 = pos1->next;
                    pos2 = pos2->next;
                }
                return pos1;
            }
            else
            {
                linklist *p1 = linklist_getenter(head1);
                linklist *p2 = linklist_getenter(head2);
                while(p1 != p2)
                {
                    if(p1->next = p1)
                    {
                        return NULL;
                    }
                    p1 = p1->next;
                }
                return p1;
            }
        }
    }
}

求两个有序链表的交集

linklist *linklist_union(linklist *head1,linklist *head2)
{
    if(head1 == NULL)
    {
        return NULL;
    }
    if(head2 == NULL)
    {
        return NULL;
    }
    else
    {
        linklist *cur1 = head1;
        linklist *cur2 = head2;
        linklist *newhead = NULL;
        linklist *newtail = NULL;
        for(;cur1 !=NULL;cur1 = cur1->next)
        {
            for(cur2 = head2;cur2 != NULL;cur2 = cur2->next)
            {
                if(cur2->data = cur1->data)
                {
                    if(newhead == newtail == NULL)
                    {
                        newtail = newhead = cur2;
                    }
                    else
                    {
                        newtail->next = cur2;
                        newtail = newtail->next;
                    }

                }
            }
        }
        return newhead;
    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值