【leetcode 两个链表的交集点】Intersection of Two Linked Lists

服务器测试之网卡性能调优 nvidia下载mlnx_ofed:https://network.nvidia.com/products/infiniband-drivers/linux/mlnx_ofed/intel下载驱动固件:https://www.intel.com/content/www/us/en/download-center/home.html。然而,你可以根据实际情况手动调整中断相关配置。老生常谈了,一些性能相关的设置项打开,节能的关闭,虚拟化相关的VMX/SR-IOV/IOMMU最好也都关掉。 阅读详情

Intersection of Two Linked Lists

 

1、题目

Write a program to find the node at which the intersection of two singly linked lists begins.


For example, the following two linked lists:

A:          a1 → a2
                   ↘
                     c1 → c2 → c3
                   ↗            
B:     b1 → b2 → b3

begin to intersect at node c1.


Notes:

  • If the two linked lists have no intersection at all, return null.
  • The linked lists must retain their original structure after the function returns.
  • You may assume there are no cycles anywhere in the entire linked structure.
  • Your code should preferably run in O(n) time and use only O(1) memory.

2、分析

题目大意:给两个链表,找出它们交集的那个节点,要求时间复杂度O(n),空间复杂度O(1)。

有以下几种思路:
(1)暴力破解,遍历链表A的所有节点,并且对于每个节点,都与链表B中的所有节点比较,退出条件是在B中找到第一个相等的节点。时间复杂度O(lengthA*lengthB),空间复杂度O(1)。

(2)哈希表。遍历链表A,并且将节点存储到哈希表中。接着遍历链表B,对于B中的每个节点,查找哈希表,如果在哈希表中找到了,说明是交集开始的那个节点。时间复杂度O(lengthA+lengthB),空间复杂度O(lengthA)或O(lengthB)。

(3)双指针法,指针pa、pb分别指向链表A和B的首节点。
遍历链表A,记录其长度lengthA,遍历链表B,记录其长度lengthB。
因为两个链表的长度可能不相同,比如题目所给的case,lengthA=5,lengthB=6,则作差得到lengthB-lengthA=1,将指针pb从链表B的首节点开始走1步,即指向了第二个节点,pa指向链表A首节点,然后它们同时走,每次都走一步,当它们相等时,就是交集的节点。
时间复杂度O(lengthA+lengthB),空间复杂度O(1)。双指针法的代码如下:

3、代码

 ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        ListNode *pa=headA,*pb=headB;
        int lengthA=0,lengthB=0;
        while(pa) {pa=pa->next;lengthA++;}
        while(pb) {pb=pb->next;lengthB++;}
        if(lengthA<=lengthB){
            int n=lengthB-lengthA;
            pa=headA;pb=headB;
            while(n) {pb=pb->next;n--;}
        }else{
            int n=lengthA-lengthB;
            pa=headA;pb=headB;
            while(n) {pa=pa->next;n--;}
        }
        while(pa!=pb){
            pa=pa->next;
            pb=pb->next;
        }
        return pa;
    }


4、题目作者的解答(英文)

作者双指针法花费的时间比我的方法更少

There are many solutions to this problem:

  • Brute-force solution (O(mn) running time, O(1) memory):

    For each node ai in list A, traverse the entire list B and check if any node in list B coincides with ai.

  • Hashset solution (O(n+m) running time, O(n) or O(m) memory):

    Traverse list A and store the address / reference to each node in a hash set. Then check every node bi in list B: if bi appears in the hash set, then bi is the intersection node.

  • Two pointer solution (O(n+m) running time, O(1) memory):
    • Maintain two pointers pA and pB initialized at the head of A and B, respectively. Then let them both traverse through the lists, one node at a time.
    • When pA reaches the end of a list, then redirect it to the head of B (yes, B, that's right.); similarly when pB reaches the end of a list, redirect it the head of A.
    • If at any point pA meets pB, then pA/pB is the intersection node.
    • To see why the above trick would work, consider the following two lists: A = {1,3,5,7,9,11} and B = {2,4,9,11}, which are intersected at node '9'. Since B.length (=4) < A.length (=6), pB would reach the end of the merged list first, because pB traverses exactly 2 nodes less than pA does. By redirecting pB to head A, and pA to head B, we now ask pB to travel exactly 2 more nodes than pA would. So in the second iteration, they are guaranteed to reach the intersection node at the same time.
    • If two lists have intersection, then their last nodes must be the same one. So when pA/pB reaches the end of a list, record the last element of A/B respectively. If the two last elements are not the same one, then the two lists have no intersections.


OpenWrt南京电信PPPoE IPv6配置:从零搭建稳定双栈网络 本文详细指导如何在南京电信PPPoE宽带环境下,为OpenWrt路由器配置稳定的IPv6双栈网络。核心在于正确设置WAN6接口为DHCPv6客户端,并请求/60前缀,同时配置LAN口分发地址并放行防火墙关键流量,确保内网设备获得原生IPv6连接。 阅读详情

相关推荐

微信支付开发之平台给用户转账到零钱的功能

请求消息中应包含商家批次单号、转账名称、appid、转账总金额、转账总笔数、转账openid、收款用户姓名等信息。批次已受理成功,若发起批量转账的30分钟后,转账批次单仍处于该状态,可能原因是商户账户余额不足等。【转账场景ID】 该批次转账使用的转账场景,如不填写则使用商家的默认场景,如无默认场景可为空,可前往“商家转账到零钱-前往功能”中申请。【转账总笔数】 一个转账批次单最多发起一千笔转账。【商家批次单号】 商户系统内部的商家批次单号,要求此参数只能由数字、大小写字母组成,在商户系统内部唯一。

程序员weixin_sodomany的博客 3461

两个链表求交集

思路:釆用归并的思想,设置两个工作指针pa和pb,对两个链表进行归并扫描,只有同时出现在两集合中的元素才链接到结果表中且仅保留一个,其他的结点全部释放。当一个链表遍历完毕后,释放另一个表中剩下的全部结点。 LinkList Union(LinkList &la,LinkList &lb) { pa=la->next; //设工作指针分别为pa和pb pb=lb->next; pc=la; //结果表中当前合并结点的前驱指针 while(..

qq_48182858的博客 5387

手把手教你使用YOLOv11模型,训练自己数据集,百分百成功(无废话)

YOLOV11百分百成功训练自己的数据集并进行图像、视频流脚本预测

chenhenGzzz的博客 1960

力扣---LeetCode160. 相交链表(代码详解+流程图)

第十弹 --- 力扣LeetCode每日一题

小沈.的博客 1840

以宠辱之戒写 SAP Fiori Elements,把标准能力当成身体,把扩展冲动当成大患

SAP Fiori Elements开发中常面临业务方追求"独特页面"的压力,但过度自定义会破坏框架价值。老子"宠辱若惊"的智慧提醒开发者:不应因用户称赞或批评而动摇架构原则。标准模板的价值在于确保业务语义、交互规范和后端服务的一致性。扩展应遵循官方API,将业务逻辑放在ABAP/CDS层而非前端控制器。成熟项目会平衡标准与定制,使用annotation表达UI行为,仅在必要时通过受支持的extension points进行增强,保持系统的长期可维护性。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 1008

深度学习之目标检测--Pytorch实战

只需要能看懂python代码,就能最快入门深度学习---目标检测。目标检测简单的玩玩儿还行,模型调优,调参,还需要一定的数学功底。通过代码来实现卷积,池化,非线性激活,正则化,归一化,能让人减少对大量数学公式的敬畏,手写神经网络也是考研复试中上机考试的压轴题,掌握搭建神经网络和如何训练模型是需要掌握的基础的

需要远程指导仿真实验、代码有问题的,请后台私信或者关注公众号 1万+

两个链表交集

题目给定两个链表,两个链表有一部分是一样,求给出两个链表交集开始的地方。 方法1 步骤1 ,求先求出两个链表的长度差,然后让 步骤2,长度较长的链表,向后移动长度差。使得两个链表后续遍历长度相同, 步骤3 ,最后移动两个指针,一次对比数据, def getIntersectionNode(headA,headB): curA,curB = headA,headB ...

qq_33465047的博客 2622

基于 Verilog 的经典数字电路设计(10)三态门

三态门(Triple-State Gate)亦称 “三态输出门”、“三态门输出电路”,是一种重要的总线接口电路,具有高电平、低电平和高阻抗三种输出状态的门电路,不过我们通常在设计中比较少见。   三态指其输出既可以是一般二值逻辑电路,即正常的高电平(逻辑 1)或低电平(逻辑 0),又可以保持特有的高阻抗状态,高阻态相当于隔断状态(电阻很大,相当于开路)。

新芯设计的博客 1万+

leetcode:相交链表(详解)

前言:内容包括-题目,代码实现,大致思路,代码解读

weixin_73142957的博客 1577

Python爬虫实战【房产刚需专属】:爬取链家/贝壳完整房源数据,精准筛选刚需房源【房价+户型+地段+面积+装修】

本次爬虫的核心亮点就是刚需专属筛选,我已经把刚需买房的主流筛选逻辑封装为独立函数,你只需要修改筛选参数,就能精准匹配你的需求无需懂Python语法,直接改数字/文字即可!本次爬虫是刚需买房+Python爬虫实战的完美结合。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 1836

基于simulink的智能驾驶领域--智能车道保持辅助系统仿真

LKAS通过摄像头等传感器检测车辆相对于车道线的位置,并自动调整转向,以帮助驾驶员保持在车道内行驶。将摄像头传感器模块的输出连接到车道线检测模块的输入端口,再将车道线检测模块的输出连接到转向控制器模块的输入端口,最后将转向控制器模块的输出连接到车辆动力学模型的转向输入端口,形成闭环控制系统。上升时间是指系统从初始转向角上升到目标转向角所需的时间,调节时间是指系统进入并保持在目标转向角一定范围内的所需时间。该控制器将根据摄像头传感器的数据调整车辆的转向角,以保持车辆在车道内行驶。

xiaoheshang_123的博客 164

[LeetCode]相交链表

[LeetCode]相交链表题目分析代码总结 题目 给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null 。图示两个链表在节点 c1 开始相交: 题目数据 保证 整个链式结构中不存在环。 注意,函数返回结果后,链表必须 保持其原始结构 。 链接:https://leetcode-cn.com/problems/intersection-of-two-linked-lists/description/ 分析 我.

Mr、Jerry的博客 551

使用Python爬取在线图书馆的图书馆藏和阅读记录

通过使用Python编写爬虫程序,我们可以从在线图书馆上获取图书馆藏和阅读记录,帮助人们更好地管理和利用图书馆资源。本文简要介绍了爬虫程序的准备工作、网页解析和数据提取、数据的保存和处理的关键步骤,并提供了一个示例代码。在实际应用中,我们需要注意遵守在线图书馆的使用规则和爬虫道德准则,确保合法、合规地进行爬取操作。以上仅为概述,实际应用的过程中可能需要根据具体需求进行适当的调整和改进。

2201_76125393的博客 746

LeetCode 160. Intersection of Two Linked Lists 题解

本题是双指针的经典应用问题,主要考察对双指针技巧的理解和使用。通过使用双指针,我们可以高效地找到两个链表的相交节点。双指针的核心思想是:使用两个指针分别指向两个链表的头节点,同时移动,当一个指针到达链表末尾时,将其指向另一个链表的头节点,继续移动,当两个指针相遇时,相遇的节点即为相交的起始节点。这种方法不仅适用于相交链表问题,还可以应用于许多其他需要比较两个链表的问题。掌握双指针的使用,对于解决这类问题非常重要。

cannonjinx的博客 4218

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

LeetCode Intersection of Two Linked Lists

Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A: a1 → a2 ↘ c

Coding for Dreams 3034

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

LeetCode 160 :Intersection of Two Linked Lists

Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists:  A: a1 → a2 ↘

哈哈的个人专栏 2408

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

160. Intersection of Two Linked Lists查找两个链表的交集Python

编写程序以查找两个单链表的交点开始的节点。 Input:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3 Output: Reference of the node with value = 8 相交节点的值为8(请注意,如果两个列表相交,则该值不得为0) 因为说了intersect Value不能为0所以line3,4给注释掉了。 代码下面有程序运行过程,方便理解测试值是图里.

weixin_44516745的博客 338

【LeetCode】25.Linked List —Intersection of Two Linked Lists 链表-两个链表的交集

Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: begin to intersect at node c1. Example 1: ...

weixin_30451709的博客 183

LeetCode--LinkedList--160. Intersection of Two Linked Lists(Easy)

160. Intersection of Two Linked Lists(Easy) 题目地址https://leetcode.com/problems/intersection-of-two-linked-lists/ Write a program to find the node at which the intersection of two singly linked lists be...

weixin_30856725的博客 207

[LeetCode] Intersection of Two Linked Lists 题解

Intersection of Two Linked Lists也是LeetCode的Linked List Tag下的一道题,网上流传着多种做法。

AllZone 460

LeetCode -- Intersection of Two Linked Lists

LeetCode -- Intersection of Two Linked Lists

我的英文站点:https://iorilan.medium.com/ 1237

【Leetcode刷题笔记】 160. Intersection of Two Linked Lists

题目思路:求两个链表是否存在交集。 交集:在当前节点到最后节点的所有值都相同 首先只能想到比较笨的暴力方法:在A和B的所有节点,遍历到最后的节点,判断是否相同。 参考了一些博客之后: 博客 明白了隐含条件,如果两个链表长度不一样时,那么交集点肯定存在在长度差的地方。 于是也就有了 解决方法1:首先求出两个链表长度,并且将较长的链表进行后移,知道达到长度相同的点。然后开始判断两个链表节点是否相同,...

知吾朱期应庆的博客 203

[leetcode]Intersection of Two Linked Lists

Intersection of Two Linked Lists   Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1

不积跬步无以至千里 362

【LeetCode】Intersection of Two Linked Lists(相交链表)

这道题是LeetCode里的第160道题。 题目讲的: 编写一个程序,找到两个单链表相交的起始节点。 如下面的两个链表: 在节点 c1 开始相交。 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3 输出:Reference of ...

『诞生之时已至,以此修正万象』 215

LeetCode 160. Intersection of Two Linked Lists

LeetCode 160. Intersection of Two Linked Lists(Easy) 主要知识点:链表、算法技巧(尾部对齐);优先级:5

温迪今天刷题了吗 325

LeetCode-Intersection of Two Linked Lists

LeetCode Java Intersection of Two Linked Lists

坟头草一米七五 356
上一篇: 【Leetcode】动态规划问题详解(持续更新)
下一篇: 【机器学习算法实现】kNN算法__手写识别——基于Python和NumPy函数库
wepon_
博客等级 码龄13年 4243粉丝 71原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值