【Leetcode】Intersection of Two Linked Lists

uniapp实战:5分钟搞定PDA激光扫码功能(附完整代码) 本文详细介绍了在Uniapp应用中快速集成工业PDA激光扫码功能的实战方案。通过解析PDA硬件扫码与手机摄像头扫码的核心机制差异,提供了完整的广播监听封装类代码,并演示了在Vue页面中的集成使用方法,帮助开发者高效解决仓储、零售等行业的条码扫描需求。 阅读详情

一原题

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

Credits:
Special thanks to @stellari for adding this problem and creating all test cases.


二 分析

先计算连个链表的长度LA和LB,他们的差是dif=abs(LA-LB);用两个指针分别指向链表头,先让指向较长链表的指针沿着的链表走dif个节点,之后两个指针一起沿着链表走,当他们指向的节点相同时(val相同&&.next相同),这个节点就是intersection。时间复杂度O(n),空间复杂度O(1)。

三 代码(java)

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if((headA==null)||(headB==null))
		return null;
	ListNode temp1,temp2;
	int count1,count2,dif;
	count1=1;count2=1;dif=0;
	temp1=headA;
	temp2=headB;
	while(temp1.next!=null){
		temp1=temp1.next;
		count1++;
	}
	while(temp2.next!=null){
		temp2=temp2.next;
		count2++;
	}
	if(temp1.val!=temp2.val)
	    return null;
	else
	{
		temp1=headA;
	    temp2=headB;
		dif=Math.abs(count1-count2);
		if(count1>count2){
			while(dif>0){
				temp1=temp1.next;
			    dif--;
			}
			while(count2>0){
				if((temp1.val==temp2.val)&&(temp1.next==temp2.next))
					return temp1;
					temp1=temp1.next;
				    temp2=temp2.next;
			}
		}
		else{
			while(dif!=0){
				temp2=temp2.next;
			    dif--;
			}
			while(count1>0){
				if((temp1.val==temp2.val)&&(temp1.next==temp2.next))
					return temp1;
					temp1=temp1.next;
				    temp2=temp2.next;
			}
		}
		return null;	
	}

    }
}


PCIe 物理层(Physical Layer)详解 PCIe物理层关键技术解析 PCIe物理层作为协议栈底层核心,承担着比特流传输、链路训练与电气特性管理等关键功能。该层采用分层架构,包含逻辑物理层(负责编解码/加扰)和电气物理层(处理信号收发)。关键技术亮点包括: 智能链路管理:通过LTSSM状态机实现11种状态转换,涵盖链路检测(Detect)、协商(Polling/Configuration)、功耗管理(L0s/L1)等全生命周期控制 高效编码方案:Gen1/Gen2采用8b/10b编码(20%开销),Gen3+升级为128b/130b(仅1.5%开销 阅读详情

相关推荐

Cartool实战:手把手教你完成静息态EEG微状态分析的组水平聚类与模板匹配

本文详细解析了使用Cartool进行静息态EEG微状态分析的组水平聚类与模板匹配全流程。从数据准备、算法选择到参数优化,提供了k-means与T-AAHC算法的实操建议,帮助研究者避开常见陷阱,获得可靠分析结果。特别针对最优类别数确定、模板匹配参数设置等核心环节进行深入探讨,适用于认知神经科学领域的EEG微状态研究。

weixin_31220401的博客 265

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 565

华北理工大学以升实验班蓝桥杯选拔赛题解

文章目录华北理工大学以升实验班蓝桥杯选拔赛题解A:期末考试题目描述题目讲解代码B:月份题目描述题目讲解代码C:打饭题目描述题目讲解代码D : 正弦波信号题目描述题目讲解代码E : 亲爱的长方形题目描述题目讲解代码F:挖金子题目描述题目讲解代码G : 金金的游戏题目描述题目讲解代码H : 区间和题目描述题目讲解代码详细视频讲解请关注华北理工大学b站账号。 华北理工大学以升实验班蓝桥杯选拔赛题解 A:期末考试 题目描述 Description 期末考试过去许多天了,如今成绩都出来了,那么现在让我们对成绩我分个类

Qq1739206233的博客 1133

[LeetCode] Intersection of Two Linked Lists 题解

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

AllZone 460

FOC-滑膜控制器SMC/滑膜观测器SMO

三相PMSM是一个非线性、强耦合的多变量控制系统,对于外界的扰动或者电机自身的参数发生变化时,传统的PI控制并不能满足实际的要求。因此可以引入滑模控制SMC,它对扰动和参数不敏感,响应速度也够快。简单来说就是,滑膜观测器就是通过采样与坐标变换得到的Ualpha,Ubeta,Ialpha,Ibeta四个参数值,获取。SMO叫滑膜观测器(Sliding Mode Observer),用于获取转子位置信息与转速信息。2、作为滑膜观测器SMO,获取电机角度与转速信息。之间的误差来设计滑膜观测器,并通过该误差。

小胡同学的工程师学习之路 1万+

结合 Conda 手动安装 RepeatMasker

RepeatMasker 是一个广泛使用的生物信息学工具,用于识别和屏蔽(mask)基因组序列中的 重复序列(repeats) 。在使用过程中需要调用重复序列数据库(Repbase 和 Dfam),如果使用Conda安装的话会出现许多问题,因此推荐手动安装。在此记录我手动安装RepeatMasker的流程,希望对各位同学有所帮助。

m0_65437087的博客 2541

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

使用Qt Creator自带的Qt Designer构造UI界面流程总结

在本文中,我们将总结使用Qt Creator自带的Qt Designer构造UI界面的流程。在对话框中,您可以选择一个控件的信号,然后将其连接到另一个控件的槽。例如,您可以将按钮的点击信号连接到一个槽函数,以响应按钮的点击事件。右键单击窗口设计器中的空白区域,选择适当的布局管理器,如水平布局、垂直布局或网格布局。在Qt Designer中,您可以使用各种预定义的UI控件来设计应用程序的界面。现在,您可以在应用程序的源代码中使用生成的UI界面。在您的源文件中,包含相应的头文件,然后创建一个UI界面的实例。

cl6789的博客 1402

LeetCode Intersection of Two Linked Lists

原题链接在这里:https://leetcode.com/problems/intersection-of-two-linked-lists/ 思路: 1. 找到距离各自tail 相同距离的起始ListNode,可以通过两个pointer,长的那个先移动Math.abs(lenA-lenB). 2. 两个pointer各自移动直到找到相同的ListNode. Node: 1. 若有inter

Dylan_Java_NYC的博客 512

uniapp - 解决报错Error in event handler for “service.requestComponentInfo“: “TypeError: cannot read prop

uniapp,报错,Error in event handler for "service.requestComponentInfo": "TypeError: cannot read properties of undefined (reading 'matches')",h5移动端网页,微信小程序,支付宝小程序,Android安卓,苹果ios,app,nvue,如何解决这个问题,一直报错,uniapp报错Error,都检查了还是报错,怎么解决这个错误,uniapp出现Error in event han

🏆 前端领域优质创作者 6383

【nnUNetv2进阶】三、nnUNetv2 自定义网络-发paper必会

对于很多朋友来说,找到nnunet的网络结构位置的代码可能比较困难,因为nnunet的网络结构从else:代码中没有网络结构的类名,那么使用的网络是哪个呢?打开预处理后的数据集中的nnUNetPlans.json文件,如下图,configurations里面有2d、3d_fullres等,这些表示nnunet的模型,下面的network_class_name指的就是训练使用的模型结构,一般都是PlainConvUNet。

AI爱好者的博客,分享计算机领域相关知识 8768

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

面向 OData V4 的 Fiori elements 为何强制使用 draft、它到底是什么、以及是否能关闭

本文深入解析了SAP Fiori elements中draft handling的核心机制与必要性。关键点包括:1)draft机制通过中间版本实现复杂编辑的安全恢复与并发控制;2)Fiori elements for OData V4强制要求draft-enabled服务支持事务编辑;3)非draft服务仅适用于只读场景;4)替代方案需承担完全自定义开发的成本。文章基于SAP官方文档,系统阐述了draft在事务型应用中的基础性作用,为架构决策提供了明确指导。

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

LeetCode-Intersection of Two Linked Lists

LeetCode Java Intersection of Two Linked Lists

坟头草一米七五 356

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

LeetCode160. Intersection of Two Linked Lists

【代码】LeetCode160. Intersection of Two Linked Lists。

叶卡捷琳堡的博客 1422

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

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

LeetCode -- Intersection of Two Linked Lists

LeetCode -- Intersection of Two Linked Lists

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

[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上最新的面试题目,求两个链表的交集,难度为easy,附个人理解解析以及题目作者的英文解析。

wepon的专栏 1万+

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

Leetcode 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 inte...

润泽的博客 226

Leetcode 160. Intersection of Two Linked Lists

思路1: brute force。非常简单nested loop,遍历两个链表,找到第一个相同的node。时间复杂度n^2,空间复杂度1。 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } .

GoodJobJasper的博客 293

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: begin to intersect at node c1. Example 1: In...

qq_35455503的博客 184

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的专栏 898

【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

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

Jason's BLOG 570
上一篇: 【Leetcode】Binary Tree Preorder Traversal
下一篇: 【Leetcode】Reorder List
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值