求两个单链表的交点(可能带环)

先简单分析:

两个单链表相交分如图几种情况:

实现如下:

//判断链表是否带环,若带环求入口点
PNode IsCircleList(PNode *pHead)
{
    if (NULL == *pHead || NULL == pHead)
    {
        return NULL;
    }
    PNode pFast = (*pHead)->next->next;
    PNode pSlow = (*pHead)->next;
    while (pFast != pSlow && NULL != pFast && NULL != pFast->next)
    {
        pFast = pFast->next->next;
        pSlow = pSlow->next;
    }
    if (NULL == pFast || NULL == pFast->next)
    {
        return NULL;
    }
    pSlow = *pHead;
    while (pSlow != pFast)
    {
        pSlow = pSlow->next;
        pFast = pFast->next;
    }
    return pSlow;
}

//求链表结点个数
size_t Size(PNode pHead)
{
    if (NULL == pHead)
    {
        return 0;
    }
    //先判断是不是带环的,带环就获取入口点
    PNode pCur = IsCircleList(&pHead);
    size_t count = 0;
    //不带环,遍历一次,计算出结点个数
    if (NULL == pCur)
    {
        while (NULL != pHead)
        {
            pHead = pHead->next;
            count++;
        }
    }
    else
    {
         //先计算环外的长度
        PNode pTmp = pHead;
        while (pTmp != pCur)
        {
            pTmp = pTmp->next;
            count++;
        }
        //计算环的长度
        pTmp = pTmp->next;
        count++;
        while (pTmp != pCur)
        {
            pTmp = pTmp->next;
            count++;
        }
    }
     return count;
}
//两个不带环单链表求交点
PNode IsIntersectTowListNoCircle(PNode *pHead1, PNode *pHead2)
{
    if (NULL == pHead1 && NULL == *pHead1)
    {
        return NULL;
    }
    if (NULL == pHead2 && NULL == *pHead2)
    {
        return NULL;
    }
    PNode pCur1 = *pHead1;
    PNode pCur2 = *pHead2;
    int idx = Size(pCur1) - Size(pCur2);
    //idx作为计数器,让长的链表先移动idx个结点
    if (idx > 0)
    {
        while (idx--)
        {
            pCur1 = pCur1->next;
        }
    }
    else
    {
        while (idx++)
        {
            pCur2 = pCur2->next;
        }
    }
    //然后他们一起向后移动,知道他们指向同一个交点
    while (pCur1 != pCur2)
    {
        pCur1 = pCur1->next;
        pCur2 = pCur2->next;
    }
    return pCur1;
}
//判断两个链表是否相交,若相交求交点
PNode IsIntersectTowList(PNode *pHead1, PNode *pHead2)
{
    if (NULL == pHead1 && NULL == *pHead1)
    {
        return NULL;
    }
    if (NULL == pHead2 && NULL == *pHead2)
    {
        return NULL;
    }
    PNode pCur1 = *pHead1;
    PNode pCur2 = *pHead2;
    pCur1 = IsCircleList(pHead1);
    pCur2 = IsCircleList(pHead2);
    //如果两个链表为不带环单链表
    if (NULL == pCur1 && NULL == pCur2)
    {
        return IsIntersectTowListNoCircle(pHead1, pHead2);
    }
    //如果两个链表带环
    if (NULL != pCur1 && NULL != pCur2)
    {
        //入口点相同说明他们在入口点或者入口点之前就相交,相当于单链表的相交求解
        if (pCur1 == pCur2)
        {
            return IsIntersectTowListNoCircle(pHead1, pHead2);
        }
        //如果是环内相交,也就是入口点不同(也可能两个带环链表不相交)
        else
        {
            //因为pCur1是第一个相遇点,如果他们是相交的则从pCur1下一个结点开始遍历,在回到pCur1之前肯定能找到pCur2若找不到,则说明没有相交
            PNode pTmp = pCur1->next;
            while (pTmp != pCur2 && pTmp != pCur1)
            {
                pTmp = pTmp->next;
            }
            //回到自己说明没有相交
            if (pTmp == pCur1)
            {
                return NULL;
            }
           //如果找到pCur2则返回一个入口点,因为整个环都是他们的交点
            else
            {
                return pCur1;
            }
        }
    }
    return NULL;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include <stdio.h> #include <stdlib.h> //定义链表结构体 struct Node { int data; struct Node *next; }; //创建链表函数 struct Node *createList(int n) { struct Node *head, *p, *q; int i, num; head = (struct Node *)malloc(sizeof(struct Node)); head->next = NULL; q = head; for (i = 0; i < n; i++) { printf("请输入第%d个节点的值:", i + 1); scanf("%d", &num); p = (struct Node *)malloc(sizeof(struct Node)); p->data = num; p->next = NULL; q->next = p; q = p; } return head; } //输出链表函数 void printList(struct Node *head) { struct Node *p; p = head->next; while (p != NULL) { printf("%d ", p->data); p = p->next; } printf("\n"); } //逆置链表函数 void reverseList(struct Node *head) { struct Node *p, *q, *r; p = head->next; q = NULL; while (p != NULL) { r = p->next; p->next = q; q = p; p = r; } head->next = q; } //找中间节点函数 struct Node *findMiddleNode(struct Node *head) { struct Node *p, *q; p = head->next; q = head->next; while (q != NULL && q->next != NULL) { p = p->next; q = q->next->next; } return p; } //找倒数第k个节点函数 struct Node *findKthNode(struct Node *head, int k) { struct Node *p, *q; p = head->next; q = head->next; while (k > 0 && q != NULL) { q = q->next; k--; } if (k > 0) return NULL; while (q != NULL) { p = p->next; q = q->next; } return p; } //删除倒数第k个节点函数 void deleteKthNode(struct Node *head, int k) { struct Node *p, *q; p = head; q = head->next; while (k > 0 && q != NULL) { p = q; q = q->next; k--; } if (k > 0) return; p->next = q->next; free(q); } //判断链表是否有环函数 int hasCycle(struct Node *head) { struct Node *p, *q; p = head; q = head; while (q != NULL && q->next != NULL) { p = p->next; q = q->next->next; if (p == q) return 1; } return 0; } //找出环的交点函数 struct Node *findCycleNode(struct Node *head) { struct Node *p, *q; int flag = 0; p = head; q = head; while (q != NULL && q->next != NULL) { p = p->next; q = q->next->next; if (p == q) { flag = 1; break; } } if (flag == 0) return NULL; p = head; while (p != q) { p = p->next; q = q->next; } return p; } //判断两个单链表是否相交函数 int isIntersect(struct Node *head1, struct Node *head2) { struct Node *p, *q; p = head1; q = head2; while (p->next != NULL) { p = p->next; } while (q->next != NULL) { q = q->next; } if (p == q) return 1; else return 0; } int main() { struct Node *head1, *head2, *p; int n, k; printf("请输入链表1的节点个数:"); scanf("%d", &n); head1 = createList(n); printf("链表1为:"); printList(head1); reverseList(head1); printf("逆置后的链表1为:"); printList(head1); p = findMiddleNode(head1); printf("链表1的中间节点为:%d\n", p->data); printf("请输入链表1的倒数第k个节点:"); scanf("%d", &k); p = findKthNode(head1, k); printf("链表1的倒数第%d个节点为:%d\n", k, p->data); deleteKthNode(head1, k); printf("删除倒数第%d个节点后的链表1为:", k); printList(head1); if (hasCycle(head1)) { p = findCycleNode(head1); printf("链表1有环,环的交点为:%d\n", p->data); } else { printf("链表1没有环\n"); } printf("请输入链表2的节点个数:"); scanf("%d", &n); head2 = createList(n); printf("链表2为:"); printList(head2); if (isIntersect(head1, head2)) { printf("链表1和链表2相交\n"); p = findCycleNode(head1); printf("链表1和链表2的交点为:%d\n", p->data); } else { printf("链表1和链表2不相交\n"); } return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值