leetcode——相交链表
发布日期:2021-05-10 03:40:06 浏览次数:11 分类:精选文章

本文共 3379 字,大约阅读时间需要 11 分钟。

������������������������������������������������

���������������������������������������������������������������������������������������������������������������������������

������������

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

������������

���������������������������������������������������������������������������

  • ������������������������

    ������������������������������������������������������������������������������������������������

  • ������������������

    ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

  • ���������������

    ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

  • ������������������

    ������������������������������������������������

  • ���������������������������������

    ���������������������������������������������������������������������������A������������������B������������������B���������������������������������������������������������������������������������������������������������������������������������������������������������������

  • ������������������

    ������������������������������������������������������������������������������������������������������������������������������������������������������������������������

  • ������������

    ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������

  • ������������������

    ������������������������C���������������������

    struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {    // ������������������������������    if (headA == NULL || headB == NULL) {        return NULL;    }    // ���������������������    int lenA = 0;    struct ListNode *currentA = headA;    while (currentA != NULL) {        lenA++;        currentA = currentA->next;    }    int lenB = 0;    struct ListNode *currentB = headB;    while (currentB != NULL) {        lenB++;        currentB = currentB->next;    }    // ������������������    if (lenA > lenB) {        int diff = lenA - lenB;        struct ListNode *currentA = headA;        while (diff-- > 0 && currentA != NULL) {            currentA = currentA->next;        }    } else if (lenB > lenA) {        int diff = lenB - lenA;        struct ListNode *currentB = headB;        while (diff-- > 0 && currentB != NULL) {            currentB = currentB->next;        }    }    // ������������������    struct ListNode *result = NULL;    while (headA != NULL && headB != NULL) {        if (headA == headB) {            result = headA;            break;        }        headA = headA->next;        headB = headB->next;    }    return result;}

    ������

    ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    上一篇:Leetcode——对称的二叉树
    下一篇:leetcode——链表的中间节点

    发表评论

    最新留言

    路过,博主的博客真漂亮。。
    [***.116.15.85]2025年04月14日 03时19分11秒