力扣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;
	}
}
相关推荐
邵奈一18 分钟前
GPT-6.1 Terra速度是Sol的3倍,价格减半
算法
RaaS10018 分钟前
Node.js Brotli RCE 漏洞自查指南:fetch、undici、axios 都在影响面,三分钟确认你的版本
人工智能·深度学习·算法
shehuiyuelaiyuehao32 分钟前
算法56,重排链表
数据结构·算法·链表
qq_452396231 小时前
第四篇:《索引原理与实战:B-tree、Hash、GIN、GiST、BRIN》
算法·哈希算法·gin
西索斯coding2 小时前
Qwen3-Max 与 Qwen-Plus:60 个真实修复任务评测方案
大数据·网络·算法·ai
INGNIGHT2 小时前
440 · 背包问题(三)(无限次数)
算法·leetcode·动态规划
suhan2 小时前
【数据结构】七、查找:1.查找的概念、线性结构查找(顺序、折半(二分)、插值、稠密、分块、倒排)
数据结构·算法·排序算法
killerbasd3 小时前
总结 10.26
算法
深圳安锐科技有限公司3 小时前
从成像尺度到亚像素解算:变焦视觉位移监测如何让远近断面都稳
图像处理·人工智能·深度学习·算法·计算机视觉·系统架构·结构健康监测