LeetCode算法题-Intersection of Two Linked Lists(Java实现)

大数据安全 | 期末复习(上)| 补档 大数据定义:无法在一定时间范围内用常规软件工具进行捕捉、管理和处理的数据集合,是需要新处理模式才能具有更强的决策力、洞察发现力和流程优化能力的海量、高增长率和多样化的信息资产。 阅读详情

这是悦乐书的第178次更新,第180篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第37题(顺位题号是160)。编写程序以找到两个单链表交叉的节点。例如:

以下两个链表:

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

链表A和链表B在c1处相交。

注意:

如果两个链接列表根本没有交集,则返回null。

函数返回后,链接列表必须保留其原始结构。

可以假设整个链接结构中没有任何环。

代码最好在O(n)时间内运行,并且只使用O(1)内存。

本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试。

02 第一种解法

这两个单链表,也可以看做是两数组,找出其中重复元素开始位置的值,最直接的办法就是上两层循环,外层遍历链表A,内层循环遍历链表B,直到遇到相交的节点为止,否则返回空。

特殊情况:当两链表中有一个为空的话,直接返回空。

public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
    if (headA == null || headB == null) {
        return null;
    }
    ListNode B = headB;
    while (headA != null) {
        while (headB != null) {
            if (headA == headB) {
                return headA;
            }
            headB = headB.next;
        }
        headA = headA.next;
        headB = B;
    }
    return null;
}

但是此种解法的时间时间复杂度是O(n^2),最坏的情况是两链表的长度都是n,并且相交的节点在最后位置或者没有相交的点。

03 第二种解法

先来看个例子,假设A和B两人要比武,A的功夫等级是10级,B的功夫等级是8级,B觉得这样比武不公平,就让A把实力压制到8级,再去和B比武,此时两人的功夫等级都是8级,于是开始了一场惊天动地的比武…

回到此题中来,因为无法确定两个链表的长度是否相等,就算两者有交点,但是起点不一样,还是会错过,无法找到交点。这时,我们就需要判断两链表的长度,重新定义好起点,才能开始遍历节点。

如果链表A长度大于链表B的长度,此时链表A就需要先向前移动两者之间长度差值个节点,然后才能和B开始依次遍历比较,反之B也是如此。

public ListNode getIntersectionNode2(ListNode headA, ListNode headB) {
    int len1 = findLength(headA);
    int len2 = findLength(headB);
    int diff = (len1 >= len2) ? (len1 - len2) : (len2 - len1);
    if (len1 >= len2) {
        while (diff > 0) {
            headA = headA.next;
            diff--;
        }
    } else {
        while (diff > 0) {
            headB = headB.next;
            diff--;
        }
    }
    while (headA != null || headB != null) {
        if (headA == headB) {
            return headA;
        }
        headA = headA.next;
        headB = headB.next;
    }
    return null;
}

public int findLength(ListNode head) {
    int len = 0;
    while (head != null) {
        len += 1;
        head = head.next;
    }
    return len;
}

04 第三种解法

还记得之前那道判断单链表是否有环的题目吗?没错,此题我们也可以借助环的思路来解题。

当遍历A链表时,如果遍历到了最后一位节点,此时跳转到B链表的起始节点,然后开始遍历B链表;当遍历B链表时,如果遍历到了最后一位节点,此时跳转到A链表的起始节点,然后开始遍历A链表。此时就相当于在遍历一条由链表A和链表B组合起来的新链表,如果此新链表存在环,那么表示两链表是有交点的,如果遍历完都没有碰到环,说明两链表是没有交点的。

public ListNode getIntersectionNode3(ListNode headA, ListNode headB) {
    if (headA == null || headB == null) {
        return null;
    }
    ListNode A = headA;
    ListNode B = headB;
    while (A != B) {
        if (A != null) {
            A = A.next;
        } else {
            A = headB;
        }
        if (B != null) {
            B = B.next;
        } else {
            B = headA;
        }
    }
    return A;
}

05 小结

此题最优解法是第三种解法,其次是第二种,最差的解法是第一种。

算法专题目前已连续日更超过一个月,算法题文章37+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

手把手教你学Simulink——无刷直流电机(BLDC)霍尔传感器(Hall Sensor)信号处理电路仿真 在中,转子位置靠​ 给出,换相逻辑(Commutation Table)据此决定。实际霍尔输出是,且常有:比较迟滞(内置或外加分压)RC 低通(防 EMI 尖刺)与 MCU 间想让你霍尔三相 UVW 理想方波(120° each, 换相每 60°elec)经观察:原始 HU/HV/HW、滤波后波形、整形后 TTL 电平验证换相表(001→100→010…)与转子转向匹配是破局关键。 阅读详情

相关推荐

小程序结合Three.js与gsap:打造流畅3D动画的完整指南

本文详细介绍了如何在小程序中整合Three.js与GSAP动画库,打造流畅的3D动画效果。通过从环境搭建、基础场景创建到模型加载与复杂动画编排的完整指南,帮助开发者克服小程序性能限制,实现如商品360度展示等沉浸式交互体验,提升产品视觉吸引力。

python9snake的博客 549

【LeetCode】题记日记 160.相交链表 Intersection of Two Linked Lists

【LeetCode】题记日记 160.相交链表 Intersection of Two Linked Lists

qq_21565101的博客 2058

PSM倾向匹配详细步骤和程序

目录1.安装psmatch2统计包。2.数据准备3.数据分析及命令解读4.结果解读4.1模型拟合结果,此处无太多实际意义。4.2试验组可匹配的观测概览,按照命令中设定的匹配规则,试验组有8例患者未能匹配到合适对照。4.3结果解读的重点应该是对stata新生成的中间变量的解读。4.4均衡性检验结果4.5匹配结果的图示化5.Stata命令汇总最后留下两个问题给大家思考:题目 试验设计中,匹配的目的在于确保干预效应估计是建立在可比个体之间的不同结果的基础上。最简单的匹配方式是将干预组和对照组中协变量值相同的两个个

平平淡淡,戒急用忍,一生学闭嘴。 5万+

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

倍福ADS通讯协议深度解析——从握手到数据包的完整旅程

本文以工程师视角,通过Wireshark抓包实战,深度解析了倍福ADS通讯协议的完整工作流程。从TCP三次握手建立连接,到拆解AMS/TCP信封、AMS Header地址与命令解析,再到ADS Data读写请求的载荷构成,完整呈现了一次数据通讯的“旅程”。文章重点剖析了协议包格式,并结合实际案例,提供了基于协议分析的故障排查思路,帮助开发者深入理解并精准调试工业自动化系统通讯。

weixin_29231725的博客 152

腾讯滑块验证码collect参数逆向与JSVMP对抗实战

本文深入解析腾讯滑块验证码collect参数的逆向过程与JSVMP虚拟机对抗技术。通过详细的环境搭建、抓包分析和JSVMP逆向技巧,揭示collect参数的生成逻辑与加密流程,并提供完整的模拟实现方案和常见问题排查方法,助力开发者突破腾讯天御验证码的防护机制。

weixin_29009501的博客 1584

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

移动硬盘报错0x80070570?5分钟搞定CHKDSK修复(附详细参数说明)

本文针对移动硬盘报错0x80070570问题,提供了详细的CHKDSK修复方法。通过5分钟核心修复流程,指导用户使用系统自带工具快速修复文件系统逻辑错误,恢复硬盘访问。文章还深入解析了CHKDSK参数,并提供了备选数据恢复方案与日常防护建议。

app77的博客 236

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

西门子博途系列学习笔记SCL(三)______自由口通讯(RS485轮询程序)

西门子博途V16系列学习笔记SCL(三)___自由口通讯轮询程序(RS485) 西门子博途系列学习笔记SCL(三)______自由口通讯(RS485轮询程序) 文章目录西门子博途V16系列学习笔记SCL(三)___自由口通讯轮询程序(RS485)前言一、FreePort自由口通讯是什么?二、使用步骤1.模块参数定义2.定义FB功能块参数:3.程序代码如下(每天写代码100行,学不会你来找我):总结 前言 很长时间都没有更新了,主要是最近在学习罗克韦尔的STUDIO5000PLC编程软件,怎么说,和博

weixin_42940283的博客 1万+

基于Python的彭博社新闻高效爬取实战:最新技术与完整代码解析

本文将详细介绍如何使用Python最新技术构建一个高效的彭博社(Bloomberg)新闻爬虫系统。我们将从爬虫基础概念讲起,逐步深入到高级反反爬技术、异步抓取、数据解析与存储等核心环节。文章包含完整的代码实现,采用最新的aiohttp、Playwright等技术栈,并特别关注法律合规性和数据质量保障。通过本文,读者将掌握一个完整的商业新闻爬虫开发流程,并能够根据实际需求进行定制化开发。关键词:Python爬虫、彭博社、Bloomberg、新闻采集、aiohttp、Playwright、反反爬技术。

2201_76125261的博客 1503

LeetCode-Intersection of Two Linked Lists

LeetCode Java Intersection of Two Linked Lists

坟头草一米七五 356

SAP-ABAP:ABAP 开发基础概念入门:写给刚踏入 SAP 开发世界的新人

本文介绍了 ABAP 开发语言的基础概念,帮助 SAP 开发新人快速入门。主要内容包括: ABAP 的发展历程与核心优势 - 作为 SAP 平台的专属语言,ABAP 在企业级应用开发中具有深度集成、数据处理高效和长期稳定的特点 ABAP 开发环境概览 - 介绍了 SE38、SE80 等关键事务码,以及 ABAP 工作台的核心组件和开发权限管理 基础语法入门 - 重点讲解了 ABAP 特有的数据类型体系(特别是结构体和内表),并通过 Hello World 示例展示程序基本结构 文章通过对比 ABAP 与 J

baidu_35680696的博客 511

【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:

DR_TITI的专栏 620

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

160. Intersection of Two Linked Lists - Java

文章目录160. Intersection of Two Linked ListsDescriptionSolutionsSubmissionsSummary 160. Intersection of Two Linked Lists https://leetcode.com/problems/intersection-of-two-linked-lists/ Description Soluti...

La La Land 216

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

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

leo_weile的博客 207

Intersection of Two Linked Lists

Intersection of Two Linked Lists Description Write a program to find the node at which the intersection of two singly linked lists begins. Example 1: A: a1 → a2 ↘ ...

mcrwayfun,欢迎你 239

【LeetCode】160. Intersection of Two Linked Lists 相交链表(Easy)(JAVA)

【LeetCode】160. Intersection of Two Linked Lists 相交链表(Medium)(JAVA) 题目地址: 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 begins. For example, the fo

qq_16927853的博客 240

[leetcode] 160. Intersection of Two Linked Lists 解题报告

题目链接: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 begins. For example, the following

小榕流光的专栏 616

leetcode之Intersection of Two Linked Lists

(1)C语言实现/** * Definition for singly-linked list. * struct ListNode { *     int val; *     struct ListNode *next; * }; */struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *he...

代码草根的专栏 257

[Leetcode 160, Easy] Intersection of Two Linked Lists

Leetcode, Easy.

算法的实践 415

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

不思量 288

【Leetcode】Intersection of Two Linked Lists in 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 ↘

qbt4juik的专栏 1066

160. Intersection of Two Linked Lists

题目链接: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 begins. For example, the following two link...

God_Mood的博客 323

leetcode 160. Intersection of Two Linked Lists

文章目录C++JavaPythonGo 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 = [

wineandchord 219

LeetCode 题解(216) : 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 ↘

451
上一篇: LeetCode算法题-Min Stack(Java实现)
下一篇: LeetCode算法题-Two Sum II - Input array is sorted
程序员小川
博客等级 码龄10年 47粉丝 305原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值