Leetcode160. 相交链表

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

给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 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;
        
    }
}
相关推荐
yi0111 小时前
DAY23: LeetCode 27 → 283:从移除元素到移动零,理解快慢指针的两种思路
人工智能·笔记·python·算法·leetcode·排序算法·双指针
程序员清风2 小时前
排序算法:从冒泡排序到归并排序
数据结构·算法·排序算法
Ada's12 小时前
【计算机基础系列】003:Python数据结构
开发语言·数据结构·python
wanderist.15 小时前
线性筛法详解:从筛质数到欧拉函数、Möbius 函数与约数函数
java·数据结构·算法
All for pursuit.15 小时前
【设计-1】208.实现Trie (前缀树)
数据结构·c++·算法·leetcode
程序员清风17 小时前
哈希表:从键值映射到高效查找
数据结构·哈希算法·散列表
Logic10120 小时前
C语言/数据结构字符串题解:最短循环节——找出能重复构成原串的最短基础子串
c语言·数据结构·字符串·枚举·时间复杂度·算法题·kmp优化
垆边人似月.21 小时前
城市连通性(200分 / 并查集)
数据结构·算法·图论
Gust of wind21 小时前
串与KMP模式匹配:存储结构、基本操作、next数组手算
c语言·数据结构·后端·算法
by2099921 小时前
list:迭代器与资源管理实现的综合叙述(下)
数据结构·c++·笔记·list