力扣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;
	}
}
相关推荐
core51234 分钟前
深度解析DeepSeek-R1中GRPO强化学习算法
人工智能·算法·机器学习·deepseek·grpo
mit6.82439 分钟前
计数if|
算法
a伊雪1 小时前
c++ 引用参数
c++·算法
圣保罗的大教堂2 小时前
leetcode 3531. 统计被覆盖的建筑 中等
leetcode
Data_agent2 小时前
1688获得1688店铺列表API,python请求示例
开发语言·python·算法
2301_764441332 小时前
使用python构建的应急物资代储博弈模型
开发语言·python·算法
hetao17338373 小时前
2025-12-11 hetao1733837的刷题笔记
c++·笔记·算法
Xの哲學3 小时前
Linux电源管理深度剖析
linux·服务器·算法·架构·边缘计算
小飞Coding3 小时前
一文讲透 TF-IDF:如何用一个向量“代表”一篇文章?
算法