LeetCode160. Intersection of Two Linked Lists

openGauss 5.0企业版主从部署,实战狂飙 阅读详情

题目:

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;
    }
};
FANUC机器人_KAREL编程入门学习(1) 阅读详情

相关推荐

Win10家庭版中文用户名改英文全攻略:从注册表到环境变量一步不落

本文详细介绍了Windows 10家庭版中文用户名改为英文的完整流程,包括注册表编辑、环境变量调整等关键步骤。通过激活管理员账户、重命名用户文件夹、修改注册表键值等操作,彻底解决因中文路径导致的软件兼容性问题,并提供常见错误解决方案和系统维护建议。

weixin_29227425的博客 433

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

YOLOv5s模型训练全流程解析:从数据准备到实战检测

本文详细解析了YOLOv5s模型训练的全流程,涵盖从环境搭建、数据准备与格式转换,到模型训练、参数调优及实战检测的完整步骤。文章提供了具体的代码示例和常见问题解决方案,旨在帮助开发者,特别是新手,高效掌握YOLOv5s模型训练的核心技术,并成功应用于实际检测任务中。

weixin_29041767的博客 341

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 567

瑞萨RA6M5驱动ST7735屏幕避坑指南:从软件SPI到显示汉字与图形的完整流程

本文详细介绍了瑞萨RA6M5单片机驱动ST7735屏幕的完整流程,涵盖硬件连接、软件SPI控制、汉字显示及图形绘制优化等关键步骤。通过避坑指南和实用代码示例,帮助开发者解决屏幕初始化、时序控制、字库移植等常见问题,实现高效稳定的显示效果。

weixin_28705087的博客 284

汇付天下聚合支付自助接入全流程详解(附JAVA/非JAVA环境配置指南)

本文详细解析了汇付天下聚合支付的自助接入全流程,涵盖JAVA与非JAVA环境下的实战配置。从接入前准备、架构理解、密钥获取,到JAVA SDK集成、核心支付代码拆解,以及Node.js/Python等环境的裸接口对接、签名生成与异步通知处理,提供了完整的避坑指南和上线检查清单,帮助开发者高效、稳健地完成支付系统集成。

wdx01234567的博客 539

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

Python自动化神器:5分钟搞定PSCAD仿真波形图导出到Word(附WPS兼容方案)

本文介绍如何利用Python自动化库快速将PSCAD仿真波形图导出到Word或WPS,大幅提升电力系统仿真工程师的工作效率。通过精准坐标定位、智能排版和批量处理功能,实现仿真报告的自动化生成,同时提供WPS兼容方案,适用于不同办公环境。

sony5的博客 219

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 ↘

哈哈的个人专栏 2410

Windows Server 2022域控环境下的MDT部署工具安装与配置避坑指南

本文详细介绍了在Windows Server 2022域控环境中安装和配置MDT(Microsoft Deployment Toolkit)的全流程,包括ADK与Windows PE的精准安装、MDT高级配置、部署工作流构建及性能优化技巧。通过实战指南帮助IT管理员实现自动化系统部署,提升企业IT运维效率,特别适用于需要静默下发系统的大规模部署场景。

weixin_28481133的博客 452

Far planner 代码系列(35)动态障碍物预处理_激光雷达部分

主要讲解动态障碍物在激光雷达部分的剔除机制

博客 1063

LeetCode 160. Intersection of Two Linked Lists 题解

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

cannonjinx的博客 4219

老版QQ原生表情gif动态

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

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

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Leetcode 160 Intersection of Two Linked Lists

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

xxxmmc的博客 624

jre9下载 jdk9

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

【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 +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的博客 204

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

温迪今天刷题了吗 326

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

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

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址