leetcode--160 intersection of two linked list

CE基础入门:游戏内存修改与脚本开发实战指南 内存扫描是计算机程序调试和逆向工程中的基础技术,通过实时监控进程内存数据来理解程序运行机制。其原理基于现代计算机将所有运行时数据存储在内存中,每个数据都有唯一的内存地址标识。这项技术的核心价值在于帮助开发者分析软件内部数据结构,调试程序逻辑,并开发自动化工具。在游戏开发、软件测试和安全研究等领域有广泛应用。以Cheat Engine(CE)为代表的工具实现了高效的内存扫描与修改功能,支持指针寻址、多级偏移计算等高级特性。通过精确的数值类型识别和扫描策略优化,可以有效定位游戏中的血量、金币等关键数据,为游戏脚 阅读详情

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.

一句话总结:寻找并返回两个单链表的交叉会和的节点。
思路:首先想到的就是两个单链表的尾部倒着看,一定是相同的,有一个后进先出的顺序。那么将两个单链表压入栈,比较栈顶,相同,出栈,继续比较,直到两个不同,那么交叉会和的就是上一个点。
但是需要的空间为O(n+m)。
此处需要的是O(1)。
那么先遍历单链表求得长度,让长的链表先走长度差的步数,然后进行每一步对比指针和值,如果都相同的就是。
时间52ms。
代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:

    int getLen(ListNode *head)//遍历得到单链表长度
    {

        int len=0;
        ListNode* ptr=head;
        if(head == NULL)
        return len;
        while(ptr != NULL)
        {
            len++;
            ptr = ptr->next;
        }
        return len;
    }

    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        int len1 = getLen(headA);
        int len2 = getLen(headB);
        if(len1==0 || len2 == 0)
        return NULL;
        int diff= (len1 > len2) ? (len1-len2) : (len2-len1);

        ListNode* ptr=NULL;
        ListNode* ptr1=NULL;
        if(len1 > len2)
        {
            ptr = headA;
            ptr1 = headB;
        }
        else
        {
            ptr = headB;
            ptr1 = headA;
        }

        for(int i=0; i < diff ; i++)
        {
            ptr = ptr->next;
        }
        ListNode* ptr2=NULL;
        while(ptr != NULL && ptr1 != NULL  )//从头开始遍历,找到第一个相同的点
        {
            if( ptr->val == ptr1->val && ptr == ptr1)//指针相同并且值相同
            {
                ptr2 = ptr;
                break;
            }
            ptr = ptr->next;
            ptr1 = ptr1->next;
        }
        return ptr2;
    }
};
思科模拟器Cisco Packet Tracer交换机的管理配置 一、交换机的管理方式:带内管理和带外管理 通过交换机的 Console 端口管理交换机属于带外管理;这种管理方式不占用交换机的网络端口,第一次配置交换机必须利用 Console 端口进行配置。 通过 Telnet、拨号等方式属于带内管理。 二、控制台配置思科交换机 1.新建拓扑图 新建拓扑,选择console线连接交换机和PC,一端接交换机的console口,一端接PC的RS 232 口(... 阅读详情

相关推荐

【通信协议】I2C总线(一主多从)

I2C总线(一主多从)之软件模拟代码详解

m0_75090944的博客 9895

LeetCode 160 Intersection of Two Linked Lists(链表相交)(Linked List)(*)

翻译写一个程序来找出两个单链表相交的开始处。例如,如下有两个链表:在节点c1处开始相交。批注: 如果两个链表根本没有相交,返回NULL。 在函数返回后链表必须保留原有的数据结构。 你可以假设在整个链结构中没有循环。 你的代码最后可以在O(n)时间和O(1)空间运行。原文Write a program to find the node at which the intersection of two

nomasp 8187

Nginx国密HTTPS实战:SM2双证书部署与TongSuo编译指南

HTTPS作为保障网络通信安全的基石,其核心在于SSL/TLS协议与公钥基础设施(PKI)。它通过非对称加密实现身份认证与密钥交换,再结合对称加密确保数据传输的机密性与完整性。在金融、政务等对自主可控有严格要求的领域,采用符合国家密码管理局标准的国密算法(SM2/SM3/SM4)替代国际通用算法(如RSA/SHA-256/AES),已成为满足安全合规要求的关键技术路径。国密SSL不仅是一套算法替换,更涉及从底层密码库到上层应用服务的完整技术栈改造。其核心价值在于实现密码技术的自主可控,提升特定场景下的安全防

weixin_29323365的博客 522

LeetCode C++ 160. Intersection of Two Linked Lists【Linked List/Two Pointers】简单

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: Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skip

memcpy0的博客 371

地图之家总目录

地图之家总目录 cesium示例 leaflet示例

GIS之家家长 1万+

【信号与系统】Multisim 仿真信号合成与分解

软件 Multisim 进行方波信号的产生、分解和合成,观察信号波形的分解与合成,加深对信号频谱的理解。

AXYZdong的博客 2万+

Day04: LeetCode. Linked List: 24, 19, 160, 142

Day04: LeetCode. Linked List: 24, 19, 160, 142

dan_dan_burger的博客 1914

OpenClaw-遇到的问题-Health check failed: gateway closed (1006 abnormal closure (no close frame)): no clos

OpenClaw网关服务安装成功但运行异常,显示健康检查失败(错误代码1006)。系统提供了Web控制界面(http://127.0.0.1:18789/)和带令牌的安全访问链接。文档建议参考故障排除指南,并提供了可选应用扩展功能(包括移动端支持)。注意运行本地代理存在安全风险,建议加固系统配置。若需启用网络搜索功能,需配置Brave搜索API密钥。安装完成后会自动打开仪表盘,需保持浏览器标签页以控制OpenClaw服务。

直到世界的尽头 1万+

Leetcode_Linked_List --160. Intersection of Two Linked Lists [easy]

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 ↘ ...

Ling的博客 244

【知识图谱】Neo4j基本操作及数据库文件导入(graph.db.dump)

初探Neo4j导入数据备份数据客户端操作导入csv文件指定csv文件格式使用命令行导入csv文件附录 请读者参考上篇博文 Neo4j安装教程 导入数据 若开启服务,先在终端停止 Neo4j 服务 ./neo4j stop 接下来,下载射雕三部曲的数据库文件 wget https://labfile.oss.aliyuncs.com/courses/1354/graph.db.dump 下载完将出现 graph.db.dump 文件: 将数据库文件导入到 Neo4j 中 首先进入neo4j 根目录下:

全网粉丝30W+,CSDN、稀土掘金人工智能领域签约作者,人工智能、大模型、Python 爬虫,商务合作,请私信沟通。 8628

电动汽车充电标准之 — IEC 61851 “电动汽车传导充电系统”简介

IEC 61851 作为电动汽车传导充电系统的核心国际标准,从通用要求、安全规范、EMC性能到具体的测试方法,为全球电动汽车充电设施的安全、可靠和互联互通提供了关键技术依据。对于制造商而言,它是产品走向全球市场的通行证;对于消费者和运营商而言,它是安全高效充电体验的基石。希望以上信息能帮助你全面了解 IEC 61851 标准。如果你对某个具体的部分(例如某种充电模式、EMC 要求或者是直流充电通信协议的细节)特别感兴趣,我可以提供更进一步的说明。

良好的开始一般都会感到困难。 1092

160. Intersection of Two Linked Lists leetcode (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:A: a1 → a2

shanshanhi的博客 467

【MATLAB代码】蚁群算法(Ant Colony Optimization, ACO)路径规划(避障)仿真程序,订阅专栏后可直接查看完整源代码

基于蚁群算法的路径规划系统。可自定义起点、终点和障碍物地图,系统通过蚁群算法自动计算最优路径,并输出路径坐标、障碍物地图和收敛曲线。完整MATLAB源代码直接运行。适用于复杂障碍环境下的路径优化问题。

985工科博士毕业,专攻定位、导航和滤波等算法研究。从业10年,主要使用MATLAB 3103

Leetcode日练笔记30 [Linked List专题] #142 #160 Linked List Cycle II & Intersection of Two Linked Lists

Leetcode 链表专题 #142 Linked List Cycle II #160 Intersections of two Linked List (Python解法)

Bittersweet_t的博客 186

假人有限元模型

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

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

leetcode-160- 相交链表(intersection of two linked list)-java

题目及测试 package pid160; /* 相交链表 编写一个程序,找到两个单链表相交的起始节点。 例如,下面的两个链表: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → ...

今天吃了吗的博客 385

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

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

LeetCode - 141. Linked List Cycle & 160. Intersection Of Two Linked Lists(链表成环问题)

LeetCode141 Linked List Cycle &amp;amp;amp;amp; 142 Linked List Cycle II &amp;amp;amp;amp;160 Intersection Of Two Linked Lists LeetCode141 Linked List Cycle(判断是否有环) LeetCode142 Linked List Cycle II(找到第一个入环节点) LeetCode160 ...

zxzxin的博客~ 341

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

leetcode刷题系列----模式2(Datastructure 链表)- 160:Intersection of Two Linked List 相交链表

leetcode刷题系列----模式2(Datastructure 链表)- 160:Intersection of Two Linked List 相交链表 Tips 更多题解请见本系列目录 此链表问题可以使用双指针解决,注意算法的复杂度为O(m+n),意味着2*(m+n)次遍历也属于该时间复杂度,不可狭隘的认为仅仅遍历m+n次链表节点。 双指针确实是一个很大的算法思想类。 Python # Definition for singly-linked list. # class ListNode: #

My_Communication的博客 966

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                    ↘      

BUILD 1312

leetcode刷题笔记(Golang)--160. Intersection of Two Linked Lists

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: begin to intersect at ...

weixin_44555304的博客 1346

LeetCode 160. Intersection of Two Linked Lists(相交链表) -- c语言

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: begin to intersect...

d_benhua的博客 481

leetcode 160. Intersection of Two Linked Lists

目录 一、问题描述 二、代码实现 1、常规解法 2、最佳解法 https://leetcode.com/problems/intersection-of-two-linked-lists/ 找到两个单链表(链表不存在环)交叉的第一个节点,要求时间O(n),空间O(1),同时不能改动链表的原有结构。 一、问题描述 测试用例: Input: intersectVal...

小景的博客 367

【Leetcode】 Intersection of two linked list

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 ↘

ljffdream的专栏 459

【Leetcode】Intersection of Two Linked List

【题目】: 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 ↘

ljffdream的专栏 408

AI项目为什么别急着Scale?扩展前必做的三件事

在系统架构中,扩展(Scale)通常意味着提升并发与资源,但在AI项目实践中,盲目扩展容易放大模型输出的不确定性,导致错误率升高且难定位。模型能力、数据质量、成本结构与基础设施仍在快速变化,扩展前必须解决可复现流程、可量化评估和可接受成本边界这三项基础问题。单机跑稳、批量任务设计、推理成本与延迟的权衡、模型版本管理,都是决定能否平滑扩展的关键环节。对于模型部署、Agent工程化等场景,先做小规模可观测验证,再逐步扩大并发与资源,才能避免“机器越多、问题越多”的困境。理解“Don't Scale Yet”的真

weixin_30475039的博客 334

LeetCode-Intersection of Two Linked Lists

LeetCode Java Intersection of Two Linked Lists

坟头草一米七五 356

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

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

知吾朱期应庆的博客 203
上一篇: Word中行与行之间存在空白,而且删不掉的解决办法
下一篇: leetcode--83&82 Remove Duplicates from sorted list I&II
evolone
博客等级 码龄14年 399粉丝 98原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值