力扣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;
	}
}
相关推荐
Adios7942 小时前
设置交集大小至少为2
数据结构·算法·leetcode
来一碗刘肉面9 小时前
栈的应用(表达式求值)
数据结构·算法
xiaowang1234shs9 小时前
怪兽轻断食技术深度测评:从断食计时引擎到AI识别算法的工程实践解析
数据库·人工智能·算法·macos·机器学习·p2p·visual studio
小果因子实验室10 小时前
量化研究--策略迁移算法1研究
算法
Web极客码10 小时前
如何用三段式确定性剪枝,为 LLM Agent 砍掉 35% 的 Token 成本?
服务器·人工智能·算法·机器学习
程序猿乐锅10 小时前
【数据结构与算法 | 第六篇】力扣1109,1094差分数组
java·算法·leetcode
wabs66611 小时前
关于图论【广度优先搜索的理论基础】
算法·图论·宽度优先
孤魂23311 小时前
逻辑回归算法
算法·机器学习·逻辑回归
weixin_3776348411 小时前
【多模型预测】 如何合理融合多个预测分值
算法·机器学习·概率论·预测·agent预测