LeetCode160. Intersection of Two Linked Lists

致远OA公文管理模块相关操作 致远oa协同办公系统 公文管理模板 相关操作 阅读详情

题目:

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.

思路:

题意:找两条链表的第一个交点。
要考虑,两链表长度不一致的问题。让长的链表先走,当两链表长度一致时,一起走,发现有节点相等,即为第一个交点。

代码:

class Solution {
public:
    int listLen(ListNode* head){
        int len=0;
        while(head){
            len++;
            head=head->next;
        }
        return len;
    }
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        int lenA=listLen(headA);
        int lenB=listLen(headB);
        int n;
        if(lenA>lenB){
            n=lenA-lenB;
            while(n--){
                headA=headA->next;
            }
        }
        else if(lenB>lenA){
            n=lenB-lenA;
            while(n--){
                headB=headB->next;
            }
        }
        while(headA && headB && headA!=headB){
            headA=headA->next;
            headB=headB->next;
        }
        return headA;
    }
};
PX4从放弃到精通(二十二):日志 文章目录前言一、下载日志二、ulog转CSV三、利用flightplot软件分析PX4的ulog日志1 windos安装2 ubuntu18.04安装四、使用PlotJuggler分析PX4日志五、设置日志记录条件六、添加自定义日志消息 前言 PX4 1.10以上固件 QGC 一、下载日志 将飞控链接地面站,在下图页面点击“刷新”,即可看到保存的日志(前提是要有TF卡) 选择需要下载的日志,点击右侧的“下载”,然后选择目录即可进行下载,如果想擦除所有的日志,可以点击右侧的“擦除全部” 二、ulog转CS 阅读详情

相关推荐

基于matlab的车牌识别(含子程序)

基于matlab的车牌识别系统一、对车辆图像进行预处理1.载入车牌图像:function [d]=main(jpg) [filename, pathname] = uigetfile({'*.jpg', 'JPEG 文件(*.jpg)'}); if(filename == 0), return, end global FILENAME %定义全局变量 FILENAME = [pathname f...

TuTu998的博客 4万+

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的博客 206

Fabric学习笔记(一)- Centos 7下的Fabric 2.3.2环境搭建

一、 环境准备 (一)、在VMware Workstation 15 安装一个Centos 7的虚拟环境,切换到图形化界面: 参考文章链接:Centos 7切换图形化界面 其中可能在yum groupinstall "X Window System"时会报错:Cannot find a valid baseurl for repo: base/7/x86_6,参考这篇文章即可:安装图形化界面报错解决 (二)、打开终端,安装所需的库: 切换root用户,输入命令:su root yum更新,输入命令:y

茕夜 2611

LeetCode力扣 160. 相交链表 Intersection of Two Linked Lists 题解代码 JavaScript

问题https://leetcode-cn.com/problems/intersection-of-two-linked-lists/ 练习使用JavaScript解答 /** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; * } */ /** * @param {ListNode} headA * @par.

Coding home - 漂流瓶jz 563

【蓝牙专题】HCI协议解析

一、概念: 很多人接触蓝牙,是从HCI开始,因为HCI是主机控制接口协议(Host Controller Interface),蓝牙命令通过HCI来发送到Controller; 1、架构 如下图,HCI通过物理接口(USB、UART等)或虚拟接口,传递数据到Controller,Controller也通过同样的接口发送接收数据,这里注意到HCI层是在L2CAP层之下,LMP层之上; HCI

蚂蚁的专栏 5069

Open3D 计算点云的主曲率

本文介绍了点云主曲率计算的方法与实现。通过分析曲面局部几何形状的主曲率(最大和最小方向),基于法线构建投影矩阵和协方差矩阵,使用奇异值分解计算曲率。代码采用Open3D实现,包括点云加载、法线估计、邻域搜索和曲率映射可视化功能。系统支持交互式点云文件选择,将曲率值映射为色彩进行直观展示,为点云处理中的特征提取和分割任务提供基础几何特征。实现过程包含了法线去中心化、协方差矩阵构建和特征值归一化等关键步骤,完整展示了从理论到实践的曲率分析流程。

点云侠的博客 543

leetcode 160. Intersection of Two Linked Lists python

leetcode 160. Intersection of Two Linked Lists python题目解法1:brutal force解法2:使用栈解法3:双指针法解法4:利用hashmap的思想 题目 Write a program to find the node at which the intersection of two singly linked lists begins. ...

qq_37821701的博客 503

深度学习论文: AnomalyDINO: Boosting Patch-based Few-shot Anomaly Detection with DINOv2

通过将DINOv2模型适应到一次性和少量异常检测中,并专注于工业应用少量异常检测。实验表明,这种方法不仅与现有技术相匹敌,甚至可以在许多设置中超越它们。我们提出的仅视觉方法AnomalyDINO基于补丁相似性,能够实现图像级别的异常预测和像素级别的异常分割。该方法在方法论上很简单,无需训练,因此不需要任何额外的数据进行微调或元学习。

mingo_敏 1903

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 ↘

哈哈的个人专栏 2406

Unity 佳能SDK 及数据获取

3. 把CameraControl 下面只要是绿色的 .cs 文件都复制到Unity 中。1. 填写信息跟官方申请SDK,大概1-2个工作日会邮件回复你。2. 将SDK这两个文件放到 Unity Plugins文件夹。

我知道你很急,但是路要一步步走 1475

在 ABAP SEGW OData 服务里,把 Function Import 参数长度校验真正跑起来

SAP Gateway服务中Function Import参数长度校验的实现机制解析:在SEGW中定义Max Length参数后,需通过模型特性USE_STRICT_FUNCTION_PARAM_CHK(7.52+版本支持)启用严格检查。该特性需在MPC_EXT的DEFINE方法中设置,框架会在URI解析阶段拦截超长参数,避免错误进入业务逻辑。对于低版本系统,建议在应用层手动添加校验。这种模型驱动的方式符合Clean Core原则,能有效提升接口健壮性。

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

LeetCode 160. Intersection of Two Linked Lists 题解

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

cannonjinx的博客 4213

Sparse-ICP—(4) 加权稀疏迭代最近点算法(matlab版)

本文介绍了加权稀疏迭代最近点算法的实现。

点云侠的博客 642

LeetCode 160之Intersection of Two Linked Lists 的Java题解

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

u012249528的专栏 896

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

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

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

Leetcode 160 Intersection of Two Linked Lists

两个指针分别从两个链表(记录为表A,表B)的表头出发,并且记录到表尾移动的步数,得到两个指针移动的步数之差。给定两个链表,找这两个链表第一个公共节点,如果没有返回nullptr。步数之差为正数,那么把表A的指针移动。然后两个指针移动到表尾,得到答案。步,否则移动表B的指针。

xxxmmc的博客 624

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

【leetcode c++】160 Intersection of Two Linked Lists

Intersection of Two Linked Lists   Write a program to find the node at whichthe intersection of two singly linked lists begins. For example, the following two linkedlists: begin to intersect

lif的博客 599

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

Leetcode +160 Intersection of Two Linked Lists

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

qq_43271202的博客 203

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

dkk37351的博客 138

LeetCode 160. Intersection of Two Linked Lists

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

温迪今天刷题了吗 324

leetcode 160. Intersection of Two Linked Lists

leetcode 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: Exampl...

Night_Thief的博客 215

LeetCode-160. Intersection of Two Linked Lists [C++][Java]

Given the heads of two singly linked-listsheadAandheadB, returnthe node at which the two lists intersect. If the two linked lists have no intersection at all, returnnull.

贫道绝缘子的博客 389

Leetcode160. Intersection of Two Linked Lists

两个链表,如果有重合的部分,把相遇点返回。如果没有重合的部分,就返回 null

magic_jiayu的博客 205

NYUer | LeetCode160 Intersection of Two Linked Lists

NYUer | LeetCode160 Intersection of Two Linked Lists

Moses_SU的博客 202
上一篇: 图论面试题
下一篇: LeetCode133. Clone Graph
_IanXiao
博客等级 码龄12年 84粉丝 241原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值