力扣hot100 相交链表 思维题

Problem: 160. 相交链表

文章目录

思路

👨‍🏫 参考题解

👩‍🏫 参考图解

复杂度

时间复杂度: O ( n + m ) O(n+m) O(n+m)

空间复杂度:

添加空间复杂度, 示例: O ( 1 ) O(1) O(1)

💖 Ac Code

Java 复制代码
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
	public ListNode getIntersectionNode(ListNode headA, ListNode headB)
	{
		if (headA == null || headB == null)
			return null;
		ListNode you = headA;
		ListNode she = headB;
		while (you != she)
		{
//			设相交段为 C 则当走到相交段时 you ==she,不相交则走到 null
			you = you == null ? headB : you.next;// A + B + C
			she = she == null ? headA : she.next;// B + A + C
		}
		return you;
	}
}
相关推荐
珠海西格电力1 小时前
数据采集与治理:零碳园区管理系统的 “生命线”
大数据·人工智能·算法·架构·能源
●VON2 小时前
HarmonyKit | 鸿蒙新特性:router 导航 API 从 pushUrl 到 UIContext 的演进
算法·华为·交互·harmonyos
星释2 小时前
鸿蒙智能体开发实战:31.鸿蒙壁纸大师 - 环境搭建与基础配置
算法·华为·ai·harmonyos·鸿蒙
ysa0510302 小时前
【板子】树上启发式合并
数据结构·c++·笔记·算法
学究天人2 小时前
数学公理体系大全:第一章 命题逻辑:真值之舞
人工智能·算法·机器学习·数学建模·动态规划·图论·抽象代数
小O的算法实验室2 小时前
2025年Neurocomputing,基于LLM的进化优化器:结合精英策略推理
算法
2301_800256113 小时前
数据结构基础期末复习例题
数据结构·算法
变量未定义~4 小时前
单调栈-四元组问题
数据结构·算法
冷小鱼4 小时前
AI Agent 核心算法:任务规划(Planning)的深度技术解析
人工智能·算法·planning
退休倒计时4 小时前
【每日一题】LeetCode 78. 子集 TypeScript
算法·leetcode·typescript