L7.【LeetCode笔记】相交链表

1.题目

. - 力扣(LeetCode)

给你两个单链表的头节点 headAheadB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null

图示两个链表在节点 c1 开始相交**:**

题目数据 保证 整个链式结构中不存在环。

注意 ,函数返回结果后,链表必须 保持其原始结构

自定义评测:

评测系统 的输入如下(你设计的程序 不适用 此输入):

  • intersectVal - 相交的起始节点的值。如果不存在相交节点,这一值为 0
  • listA - 第一个链表
  • listB - 第二个链表
  • skipA - 在 listA 中(从头节点开始)跳到交叉节点的节点数
  • skipB - 在 listB 中(从头节点开始)跳到交叉节点的节点数

评测系统将根据这些输入创建链式数据结构,并将两个头节点 headAheadB 传递给你的程序。如果程序能够正确返回相交节点,那么你的解决方案将被 视作正确答案

示例 1:

复制代码
输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
输出:Intersected at '8'
解释:相交节点的值为 8 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,6,1,8,4,5]。
在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。
— 请注意相交节点的值不为 1,因为在链表 A 和链表 B 之中值为 1 的节点 (A 中第二个节点和 B 中第三个节点) 是不同的节点。换句话说,它们在内存中指向两个不同的位置,而链表 A 和链表 B 中值为 8 的节点 (A 中第三个节点,B 中第四个节点) 在内存中指向相同的位置。

示例 2:

复制代码
输入:intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
输出:Intersected at '2'
解释:相交节点的值为 2 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [1,9,1,2,4],链表 B 为 [3,2,4]。
在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。

示例 3:

复制代码
输入:intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
输出:No intersection
解释:从各自的表头开始算起,链表 A 为 [2,6,4],链表 B 为 [1,5]。
由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。
这两个链表不相交,因此返回 null 。

提示:

  • listA 中节点数目为 m
  • listB 中节点数目为 n
  • 1 <= m, n <= 3 * 104
  • 1 <= Node.val <= 105
  • 0 <= skipA <= m
  • 0 <= skipB <= n
  • 如果 listAlistB 没有交点,intersectVal0
  • 如果 listAlistB 有交点,intersectVal == listA[skipA] == listB[skipB]

进阶: 你能否设计一个时间复杂度 O(m + n) 、仅用 O(1) 内存的解决方案?

代码模版

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) 
{
}

2.自解

cpp 复制代码
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) 
{
    int length_list1=0;
    int length_list2=0;
    struct ListNode* cur1=headA;
    struct ListNode* cur2=headB;
    if (cur1==cur2)
    return cur1;
    if (headA==NULL||headB==NULL)
       return NULL;
    //计算两个链表的节点个数
        while(cur1)
    {
        length_list1++;
        cur1=cur1->next;
    }
        while(cur2)
    {
        length_list2++;
        cur2=cur2->next;
    }
    //恢复cur1和cur2的初值
    cur1=headA;
    cur2=headB;
    if (length_list1==length_list2)
    {
        while(cur1->next!=cur2->next)
        {
            cur1=cur1->next;
            cur2=cur2->next;
        }
        return cur1->next;
    }
    else if (length_list1>length_list2)
    {
        int delta_length=length_list1-length_list2;
        //让长链表先走delta_length步
        while (delta_length)
        {
            delta_length--;
            cur1=cur1->next;
        }
        if (cur1==cur2)
        return cur2;
        while(cur1->next!=cur2->next)
        {
            cur1=cur1->next;
            cur2=cur2->next;
        }
        return cur1->next;
    }
    else
    {
        int delta_length=length_list2-length_list1;
        //让长链表先走delta_length步
        while (delta_length)
        {
            delta_length--;
            cur2=cur2->next;
        }
        if (cur2==cur1)
        return cur1;
        while(cur1->next!=cur2->next)
        {
            cur1=cur1->next;
            cur2=cur2->next;
        }
        cur1=cur1->next;
        return cur1;
    }
    return NULL;
}

几个容易忽视的问题

1.在用cur1和cur2遍历链表后,不要忘记恢复cur1和cur2的初始值

2.headA和headB有一个为NULL,则返回NULL

3.在 while (delta_length)和while(cur1->next!=cur2->next)循环之间有一个if (cur1==cur2) return cur2; 在 while (delta_length)和while(cur1->next!=cur2->next)循环之间也有一个if (cur1==cur2) return cur1;这是应对特殊情况

运行结果

相关推荐
05候补工程师13 小时前
【408计网笔记】传输层与应用层高频考点:TCP/UDP特性、端口映射与交互逻辑
网络·经验分享·笔记·网络协议·tcp/ip·考研·udp
阿Y加油吧13 小时前
二刷 LeetCode:152. 乘积最大子数组 & 416. 分割等和子集 复盘笔记
笔记·算法·leetcode
papership21 小时前
【入门级-数据结构-3、特殊树:完全二叉树的数组表示法】
数据结构·算法·链表
码途漫谈21 小时前
Easy-Vibe开发篇阅读笔记(四)——前端开发之结合 Agent Skills 美化界面
人工智能·笔记·ai·开源·ai编程
smj2302_7968265221 小时前
解决leetcode第3911题.移除子数组元素后第k小偶数
数据结构·python·算法·leetcode
Beginner x_u1 天前
链表专题:JS 实现原理与高频算法题总结
javascript·算法·链表
糖炒栗子03261 天前
【笔记】高分卫星影像 TIF 切片处理
笔记
Nice_Fold1 天前
Kubernetes DaemonSet、StatefulSet与Service(自用笔记)
笔记·容器·kubernetes
_深海凉_1 天前
LeetCode热题100-寻找两个正序数组的中位数
算法·leetcode·职场和发展
踩坑记录1 天前
leetcode hot100 寻找两个正序数组的中位数 hard 二分查找 双指针
leetcode