力扣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;
	}
}
相关推荐
信奥卷王7 小时前
[GESP202609 六级] 分树规划
算法
夏幻灵7 小时前
LeetCode 21:合并两个有序链表——双指针与哑节点详解
算法·leetcode·链表
徐小黑ACG9 小时前
Golang 基础05 结构体struct
开发语言·算法·golang
Navigator_Z9 小时前
LeetCode //C - 1286. Iterator for Combination
c语言·算法·leetcode
北极有牛10 小时前
cpp学习笔记--常量指针
java·开发语言·算法
GPU实战笔记10 小时前
云端 GPU 训练的账户余额提醒:它能说明什么,不能说明什么?
深度学习·算法·成本管理·gpu云计算·云端训练
浮链序10 小时前
怎么证明"你这个模型是偷我的"——把蒸馏变成取证工具
算法·安全·llm
小O的算法实验室10 小时前
IEEE TMC,面向异构物联网的多目标量子行为拓扑优化算法
算法
圣保罗的大教堂11 小时前
leetcode 301. 删除无效的括号 困难
leetcode