码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode-160. Intersection of Two Linked Lists [C++][Java]


    LeetCode-160. Intersection of Two Linked ListsLevel up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.https://leetcode.com/problems/intersection-of-two-linked-lists/

    Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.

    For example, the following two linked lists begin to intersect at node c1:

    The test cases are generated such that there are no cycles anywhere in the entire linked structure.

    Note that the linked lists must retain their original structure after the function returns.

    Custom Judge:

    The inputs to the judge are given as follows (your program is not given these inputs):

    • intersectVal - The value of the node where the intersection occurs. This is 0 if there is no intersected node.
    • listA - The first linked list.
    • listB - The second linked list.
    • skipA - The number of nodes to skip ahead in listA (starting from the head) to get to the intersected node.
    • skipB - The number of nodes to skip ahead in listB (starting from the head) to get to the intersected node.

    The judge will then create the linked structure based on these inputs and pass the two heads, headA and headB to your program. If you correctly return the intersected node, then your solution will be accepted.

    Example 1:

    Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
    Output: Intersected at '8'
    Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect).
    From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,6,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
    

    Example 2:

    Input: intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
    Output: Intersected at '2'
    Explanation: The intersected node's value is 2 (note that this must not be 0 if the two lists intersect).
    From the head of A, it reads as [1,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.
    

    Example 3:

    Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
    Output: No intersection
    Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
    Explanation: The two lists do not intersect, so return null.
    

    Constraints:

    • The number of nodes of listA is in the m.
    • The number of nodes of listB is in the n.
    • 1 <= m, n <= 3 * 104
    • 1 <= Node.val <= 105
    • 0 <= skipA < m
    • 0 <= skipB < n
    • intersectVal is 0 if listA and listB do not intersect.
    • intersectVal == listA[skipA] == listB[skipB] if listA and listB intersect.

    Follow up: Could you write a solution that runs in O(m + n) time and use only O(1) memory?

    【C++】

    1. /**
    2. * Definition for singly-linked list.
    3. * struct ListNode {
    4. * int val;
    5. * ListNode *next;
    6. * ListNode(int x) : val(x), next(NULL) {}
    7. * };
    8. */
    9. class Solution {
    10. public:
    11. ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
    12. ListNode *l1 = headA, *l2 = headB;
    13. while (l1 != l2) {
    14. l1 = l1? l1->next: headB;
    15. l2 = l2? l2->next: headA;
    16. }
    17. return l1;
    18. }
    19. };

    【Java】

    1. /**
    2. * Definition for singly-linked list.
    3. * public class ListNode {
    4. * int val;
    5. * ListNode next;
    6. * ListNode(int x) {
    7. * val = x;
    8. * next = null;
    9. * }
    10. * }
    11. */
    12. public class Solution {
    13. public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
    14. ListNode l1 = headA, l2 = headB;
    15. while (l1 != l2) {
    16. l1 = l1 != null ? l1.next : headB;
    17. l2 = l2 != null ? l2.next : headA;
    18. }
    19. return l1;
    20. }
    21. }

    相关题目

    《剑指offer》52--两个链表的第一个公共结点[C++]_贫道绝缘子的博客-CSDN博客NowCoderLeetCode题目描述输入两个链表,找出它们的第一个公共结点。struct ListNode {int val;struct ListNode *next;ListNode(int x) : val(x), next(NULL) {}};解题思路关键:如果有公共结点,两个链表从第一个公共结点开始,之后所有的结点都是重合的,不可能再出现分叉。...https://blog.csdn.net/qq_15711195/article/details/97130651

  • 相关阅读:
    船舶稳定性和静水力计算——绘图体平面图,静水力,GZ计算(Matlab代码实现)
    复习Day01:数组part01:701. 二分查找、35. 搜索插入位置、367. 有效的完全平方数、69. x的平方根、74. 搜索二维矩阵
    非零基础自学Java (老师:韩顺平) 第4章 运算符 4.5 赋值运算符 && 4.6 三元运算符 && 4.7 运算符优先级
    Gradient conjugate priors and multi-layer neural networks
    基于springboot车辆充电桩设计与实现的源码+文档
    stm32外部时钟为12MHZ,修改代码适配
    containerd 镜像构建工具 -- nerdctl 和 buildkit
    动态规划完全背包
    「百川智能」获22亿元融资,腾讯、阿里、小米等参投
    基础算法(排序、二分、精度运算)
  • 原文地址:https://blog.csdn.net/qq_15711195/article/details/126190342
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号