Leetcode160. 相交链表

力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

给你两个单链表的头节点 headAheadB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null

题解:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

代码如下:

java 复制代码
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null || headB == null){
            return null;
        }
        ListNode head1 = headA;
        ListNode head2 = headB;
        while(head1 != head2) {
            head1 = (head1 == null) ? headB : head1.next;
            head2 = (head2 == null) ? headA : head2.next;
        }
        return head1;
        
    }
}
相关推荐
All for pursuit.14 小时前
【栈-5】84.柱状图中最大的矩形
数据结构·c++·算法·leetcode
渡我白衣14 小时前
HttpRequest与HttpResponse的实现
服务器·数据结构·c++·人工智能·tcp/ip·机器学习·caffe
晴天的雨.99217 小时前
【C++算法】和为s的两个数
开发语言·数据结构·c++·算法
无敌贵点大王1 天前
RTThread学习记录11——RT-Thread 设备模型吃透:UART/ADC/PWM/PIN 到底有什么区别?
c语言·stm32·学习·链表
淡海水1 天前
13-04-面试-源码级深度追问链
数据结构·unity·面试·c#·游戏引擎·源码·il2cpp
Logic1011 天前
C语言/数据结构位运算题解:异或XOR找出时尚聚会中的“独特颜色“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
2401_839080541 天前
C++常见八股
数据结构·c++·算法
暮雨封夕1 天前
跳表(Skip List)详解:原理、实现、使用场景与实际应用
数据结构
miller-tsunami1 天前
排序的相关知识点
数据结构·排序算法
Logic1011 天前
C语言/数据结构滑动窗口题解:替换k个字符后的最长连续相同字符子串(LeetCode 424)
c语言·数据结构·字符串·滑动窗口·时间复杂度·频率统计·算法题